我想將此查詢的結果合併到一個列表中的所有數據。 '聯盟'沒有幫助...合併SQL查詢的結果
有什麼想法嗎?
DECLARE @cnt INT = 0
DECLARE @SQLQuery AS NVARCHAR(500)
DECLARE @items AS VARCHAR(500)
DECLARE @stline AS VARCHAR(500)
while @cnt < 101
begin
set @items = 'LG_0' + convert(nvarchar(3),@cnt) + '_ITEMS'
set @stline = 'LG_0' + convert(nvarchar(3),@cnt) + '_01_STLINE'
if (OBJECT_ID (@items) is not null and OBJECT_ID(@stline) is not null)
set @SQLQuery = 'SELECT
ITM1.CODE, itm1.NAME, max (stl.date_) [latest date] , price as [latest price], '+ convert(nvarchar(3),@cnt) + ' as [proje kodu]
FROM ' + @items + ' ITM1 INNER JOIN ' + @stline + ' STL
ON ITM1.LOGICALREF = STL.STOCKREF
WHERE PRICE > 0
AND TRCODE = 1
AND CANCELLED = 0
AND INVOICEREF > 0
group by code, name, stl.DATE_ , price
'
exec (@SQLQuery)
set @cnt = @cnt +1
end
的結果是這樣的:
,但我希望他們所有聯盟-ED ......(對不起,大多是代碼的問題,我要在這裏添加更多的打字)
蜜獾回答下面你的問題。我建議的另一種方法是使用臨時表並將結果插入它。 – DVT