简体   繁体   中英

Insert variable to temp table

How can I insert declared var into temp table?

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. 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. The statement has been terminated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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