我想將多個查詢的結果合併到1個列或變量中。 我有這個疑問: -SQL - 在1個變量或列中合併多個查詢的結果
select Id from EmployeeDetail where Code = 'ABC1'
select Id from EmployeeDetail where Code = 'ABC2'
... So On till ABC200
我想所有的ID在1個可變進一步使用它。
我想用foreach來得到這個。如何在1變量中使用它以用於更進一步的查詢。
我已經嘗試了下面的例子來獲取1個串聯變量中的一個字符串: - 下面的代碼只是一個試驗,它不是在實際查詢上開火。
declare @num int
declare @num1 VARCHAR(MAX)
set @num = 1
declare @results table (val int)
while (@num < 84)
begin
insert into @results (val) values (@num)
set @num = @num + 1
set @num1 += '5000'
select @num1
end
你想Id值爲1,2,3這樣的..? – Mansoor
如果您希望所有行都位於單個字符串中,請參閱(http://stackoverflow.com/questions/15477743/listagg-in-sqlserver)。但是你打算在另一個查詢中使用它......那麼組合形式對你真的很有用嗎?您可能想要迭代第一個結果集。 –
添加最後的預期輸出是什麼?你想得到結果或者在共同分離時分配varibale –