Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store select statement result to table variable in sql server

Tags:

sql

sql-server

Declare @T_variable table(name varchar(200))
SET @T_variable =(SELECT au_lname FROM Testing)

Error Message. Msg 137, Level 16, State 1, Line 2 Must declare the scalar variable "@T_variable".

Note :- select statement result will give multiple rows.

I try to capture the select result in table variable.But i failed. Is there any way to capture the select result to Table variable Dynamically.

Thanks in advance.

like image 799
MAS Avatar asked Dec 12 '22 09:12

MAS


2 Answers

Please try below query instead since you have declared a table variable instead of a datatype variable.

Declare @T_variable table(name varchar(200))
insert into @T_variable 
SELECT au_lname FROM Testing
like image 148
TechDo Avatar answered Feb 23 '23 00:02

TechDo


Try this:

SET @T_variable :=(SELECT au_lname FROM Testing)

Adding a colon may help here.

like image 33
Aitori Avatar answered Feb 23 '23 00:02

Aitori