[英]How to use OUTPUT clause with SELECT/INTO statement
I have the following query我有以下查询
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
INTO #Jedi
which CREATE
the temporary table #Jedi
and INSERT
the data inside it. CREATE
临时表#Jedi
并INSERT
其中的数据。
I would like to use OUTPUT
to show the data I am saving inside the table but I can't understand how to use the OUTPUT
clause without getting an error message我想使用
OUTPUT
来显示我保存在表中的数据,但我无法理解如何使用OUTPUT
子句而不收到错误消息
"Incorrect syntax near 'output'."
“'输出'附近的语法不正确。”
In an INSERT INTO
query I would write the following在
INSERT INTO
查询中,我会写以下内容
INSERT INTO #Jedi([Jedi_names],[Jedi_surname])
OUTPUT INSERTED.*
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
but this just INSERT
the line if the table has been already created..但是如果表已经创建,这只是
INSERT
行..
Is it possibile to use the OUTPUT
clause in the first query?是否可以在第一个查询中使用
OUTPUT
子句?
You can't use the output
clause in a select
statement.不能在
select
语句中使用output
子句。 It's only applicable in insert
, update
, delete
and merge
.它仅适用于
insert
、 update
、 delete
和merge
。
An alternative option would be to do it in two parts: First, create the temporary table with no records:另一种选择是分两部分进行:首先,创建没有记录的临时表:
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
INTO #Jedi
WHERE 1=0
Then, insert the records into the temporary table using insert...output...select
:然后,使用
insert...output...select
将记录插入到临时表中:
INSERT INTO #Jedi
OUTPUT INSERTED.*
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
Or simply use select into
and then select
:或者简单地使用
select into
然后select
:
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
INTO #Jedi;
SELECT [Jedi_names], [Jedi_surname]
FROM #Jedi;
Please use like below请使用如下
CREATE TABLE #Jedi([Jedi_names] VARCHAR(20),[Jedi_surname] VARCHAR(20))
INSERT INTO #Jedi([Jedi_names],[Jedi_surname])
OUTPUT INSERTED.*
SELECT * FROM
(
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
)K
OUTPUT输出
Jedi_names Jedi_surname
Luke Skywalker
This is the syntax you're after:这是您所追求的语法:
CREATE TABLE #Jedi ([Jedi_forename] varchar(50),
[Jedi_surname] varchar(50));
INSERT INTO #Jedi ([Jedi_forename],[Jedi_surname])
OUTPUT Inserted.*
SELECT 'Luke' AS [Jedi_names], 'Skywalker' AS [Jedi_surname]
DROP TABLE #Jedi;
You can't use OUTPUT
and INTO
in the same statement.不能在同一个语句中使用
OUTPUT
和INTO
。
select into
will avoid the logging. select into
将避免日志记录。 The moment you use the insert into
then it defeats the purpose.当您使用
insert into
它就违背了目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.