2013-10-15 113 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,轉換....)