INSERT 语句不能包含 SELECT 语句 -sql server2012 [英] INSERT statement cannot contain a SELECT statement -sql server2012
问题描述
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
我遇到了这个错误
INSERT 语句不能包含赋值的 SELECT 语句变量的值.
An INSERT statement cannot contain a SELECT statement that assigns values to a variable.
我搜索了这个问题,但很多人使用了这样的查询,他们说没有问题!我正在使用 sql server 2012,这是 MSSQL2012 和 MSSQL2008 之间的尊重吗?如果我想从我的存储过程中返回一个表,最好的解决方案是什么?我的查询有什么问题?
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?
推荐答案
根据问题中提供的信息,这些变量似乎是不必要的.您可以像这样直接执行 INSERT...SELECT
:
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
这篇关于INSERT 语句不能包含 SELECT 语句 -sql server2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!