0

I've made a stored procedure and got the error message below, dont know why. I've been looking around for some answers and with some other guys here at the office but they're all unsure of the problem. Hoping someone here has had the same problem and knows the solution to this.

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.

Here is my Code

 Set @SQLstring = 
 'Update #TempTable set Col1' + case when len(Convert(Varchar, (4+@counter)))=1 
 then '0' else '' end 
 + Convert(Varchar,(4+@counter)) + '=''' + 
 (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,10+@somevariable * 12)
 exec('' + @SQLstring +'')
4

3 回答 3

2

如果您要构建一个 SQL 字符串并使用动态 SQL 执行它,那么您需要将其视为一个字符串

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

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

 + ( select Convert(Varchar,count(distinct Table.Column1)) from databse.Table....
于 2013-10-15T12:43:36.803 回答
0

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

于 2013-10-15T13:11:13.410 回答
0

在单独的选择查询中将 year 、 month 、 count 在下面的部分中。

(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')) 
于 2013-10-15T12:54:41.210 回答