2013-10-15 116 views
0

我做了一个存储过程,并得到了下面的错误消息,不知道为什么。我一直在四处寻找一些答案,并与办公室的其他人在一起,但他们都不确定这个问题。希望这里的某个人有同样的问题,并且知道解决方案。子查询不存在,存储过程

Msg 116, Level 16, State 1, Procedure Name_Stored_Procedure, 
Line 113 Only one expression can be specified in the select list 
when the subquery is not introduced with EXISTS. 

这里是我的代码

Set @SQLstring = 
'Update #TempTable set Col1' + case when len(Convert(Varchar, ([email protected])))=1 
then '0' else '' end 
+ Convert(Varchar,([email protected])) + '=''' + 
(select @Year, @Month, 
Convert(Varchar,count(distinct Table.Column1)) 
from Databse.Table 
where DATEPART(yy,Time) = @Year 
and DATEPART(mm,Time) = @Month 
and Table.Column2 = @Column2 and Column3 in ('X','Z','Y - A')) 
+''' where row = ' + CONVERT(varchar,[email protected] * 12) 
exec('' + @SQLstring +'') 

回答

2

如果您要构建SQL字符串,然后使用动态SQL执行它,那么你需要把它作为一个字符串

Set @SQLstring = 
    'Update #TempTable set Col' 
      + case when len(Convert(Varchar, ([email protected])))=1 then '0' else '' end 
    ... 

在您的内部选择中,从结果中删除@year, @month

+ (select Convert(Varchar,count(distinct Table.Column1)) from databse.Table.... 
+0

谢谢,对不起,它被视为一个字符串。我只是忘了在描述中输入它。尽管如此,仍然有相同的问题。 干杯 – SterlinkArcher

+0

@andrejcurcic请参阅编辑。 – podiluska

0

将年份,月份计入以下部分中单独的选择查询中。

(select @Year, @Month, 
Convert(Varchar,count(distinct Table.Column1)) 
from Databse.Table 
where DATEPART(yy,Time) = @Year 
and DATEPART(mm,Time) = @Month 
and Table.Column2 = @Column2 and Column3 in ('X','Z','Y - A')) 
0

选择CONCAT(@year,@month,转换....)