Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

    declare @us table(name nvarchar(100),lastname nvarchar(100),UID bigint,available bit);
        declare
        @Name nvarchar(100),
        @lastname nvarchar(100),
        @UID bigint,
        @Avail bit

        insert into @us
        select @name=name,@lastname=lastname,@UID=UID,@Avail=available from Users where available='1'
 select * from @us

I got this error

An INSERT statement cannot contain a SELECT statement that assigns values to a variable.

I searched for this problem but many people used queries like this and they said there is no problem! i'm using sql server 2012, is it a deference between MSSQL2012 and MSSQL2008? and what is the best solution if I want to return a table from my Stored Procedures? what is wrong in my query?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
392 views
Welcome To Ask or Share your Answers For Others

1 Answer

Based on the information provided in the question, the variables seem to be unnecessary. You can directly do INSERT...SELECT like so:

declare @us table(name nvarchar(100),lastname nvarchar(100),UID bigint,available bit);

insert into @us
select name,lastname,UID,available 
from Users 
where available='1'

select * from @us

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...