简体   繁体   English

将变量插入临时表

[英]Insert variable to temp table

How can I insert declared var into temp table? 如何将声明的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

Remove the GO statement. 删除GO语句。 It separates the query into two batches, and variables are only scoped to the batch they're created in. 它将查询分为两个批处理,变量仅限于它们创建的批处理。

You also don't need the sub-select. 您也不需要子选择。 Just do the following: 只需执行以下操作:

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

Make the following changes to the query 对查询进行以下更改

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

     if @ConcatStringlength exceed the current  Item2 VARCHAR(200)
  • else error : String or binary data would be truncated. else error:字符串或二进制数据将被截断。 The statement has been terminated. 该语句已终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM