動的 SQL を実行する proc にこのクエリを渡すと、エラーが発生します。
"select @display = @display + gp2.ProblemDesc + ''|' '
from (SELECT gp.GrantProblemID, gp.ProblemDesc, gps.GrantProblemStatus, row_number() over (partition by gp.grantproblemid order by p.periodid desc) ranking
from MergedProgressReport.dbo.[PeriodAgencyGrantProblems] pagp
inner join MergedProgressReport.dbo.PeriodAgencyGrants pag on pag.PeriodAgencyGrantsID = pagp.PeriodAgencyGrantID
and pag.GrantNumber = ''2011UMWX0001''
inner join MergedProgressReport.dbo.periodagencyinfo pai on pag.PeriodAgencyInfoID = pai.PeriodAgencyInfoID
inner join MergedProgressReport.dbo.Periods p on p.PeriodID = pai.PeriodID
inner join MergedProgressReport.dbo.GrantProblemStatus gps on pagp.GrantProblemStatusID = gps.GrantProblemStatusID
inner join MergedProgressReport.dbo.GrantProblems gp on pagp.GrantProblemID = gp.GrantProblemID
and gp.GrantProblemAreaID = 7) xx right join MergedProgressReport.dbo.GrantProblems gp2 on xx.GrantProblemID = gp2.GrantProblemID and xx.ranking = 1
where gp2.GrantProblemAreaID = 7 "
私の手続き:
CREATE proc [dbo].[Review_Get_PrePopValue](@sqlQuery nvarchar(500), @display nvarchar(200) OUTPUT)
as
EXEC sp_executesql @sqlQuery,
N'@display varchar(200) output',
@display = @display output