-1

在我的存储过程中,我将结果连接成一个字符串,例如:

Problem1|Problem2|Problem3

当我尝试存储在变量@display中时,它返回 null。当我运行查询而不将其存储到变量时,它会返回结果。不知道为什么它返回null?

declare @display nvarchar(500)

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

Select @display
4

1 回答 1

1

试试这个。可能您需要初始化变量。

declare @display nvarchar(500)
set @display = ''  

简而言之,您set在声明变量后编写查询之前缺少该语句。

于 2013-03-03T12:26:12.717 回答