2017-06-27 73 views
1

如何將聲明的var插入臨時表中?將變量插入臨時表

DECLARE @ConcatString VARCHAR(4000) 
SELECT @ConcatString = COALESCE(@ConcatString + ', ', '') + LanguageName FROM EmployeeLanguage where EmployeeId=10504 
SELECT @ConcatString AS Language 

GO 

DECLARE @T1 TABLE (
Item1 BigInt, 
Item2 VARCHAR(200) 
) 
INSERT INTO @T1 select 1,(SELECT @ConcatString AS Language) as t 

select * from @T1 

回答

3

刪除GO聲明。它將查詢分爲兩批,變量僅限於它們創建的批次。

您也不需要子選擇。只要做到以下幾點:

Insert Into @T1 
     (Item1, Item2) 
Select 1, @ConcatString; 
0

進行以下更改查詢

1. Remove the keyword GO. 
2. Item2 VARCHAR(200) column size to VARCHAR(4000) 

    if @ConcatStringlength exceed the current Item2 VARCHAR(200) 
  • 別的錯誤:字符串或二進制數據將被截斷。 該聲明已被終止。