1
enter image description hereクエリは、単一の行にスキルのリストを動的に作成します。 .How私はビューでそれを使用するように変更し、まだダイナミックsqlでのDynaimicクエリとそのビューでの実装方法
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(col + '_' + cast(seq as
varchar(2)))
from
(
select seq = row_number() over(partition by Skill.ID_Job
order by Skill.ID_Job)
from dbo.Job_Skill Skill
Where Skill.Min_Job = 1 and Skill.Ideal_Job = 1
) hr
cross apply
(
select 'Skill',1
) c (col, val)
order by val , seq
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = N'SELECT ID_Job, ' + @cols + N'
from
(
select hr.ID_Job,
col = c.col + ''_'' + cast(seq as varchar(2)),
c.val
from
(
select Skill.ID_Job,
Skill.ID_SKL_Job,
Skill.Min_Job,
Skill.Ideal_Job,
seq = row_number() over(partition by Skill.ID_Job
order by Skill.ID_Job)
from dbo.Job_Skill Skill
Where Skill.Min_Job = 1 and Skill.Ideal_Job = 1
) hr
cross apply
(
select ''Skill'', ID_SKL_Job
) c (col, val)
) x
pivot
(
max(val)
for col in (' + @cols + N')
) p
order by ID_Job'
exec sp_executesql @query
助けてくれてありがとう –
@paramtrivedi、「助けてくれてありがとう」と言っても大変親切ですが、回答を受け入れるのは親切でもあります。 [これを読んでください:誰かの回答](http://stackoverflow.com/help/someone-answers)、thx! – Shnugo