Output子句在使用表别名时返回错误。

huangapple go评论63阅读模式
英文:

Output clause in delete statement returns an error when using a table alias

问题

create table Test (a varchar(11));

create table Test2 (a varchar(11));

insert into Test values('a'),('b'),('c');

This one works just fine

delete from Test output deleted.a into Test2(a)

However this one doesn't work

delete t from Test t output deleted.a into Test2(a)

Incorrect syntax near 'OUTPUT'.

I want to be able to use a table alias there since the actual query is something like this:

DELETE t
FROM Test t
LEFT JOIN blah blah
WHERE blah blah
OUTPUT DELETED.a INTO Test2(a)

英文:
create table Test (a varchar(11));

create table Test2 (a varchar(11));

insert into Test values('a'),('b'),('c');

This one works just fine

delete from Test output deleted.a into Test2(a)

However this one doesn't work

delete t from Test t output deleted.a into Test2(a)

> Incorrect syntax near 'OUTPUT'.

I want to be able to use a table alias there since the actual query is something like this:

DELETE t 
FROM Test t 
LEFT JOIN blah blah
WHERE blah blah
OUTPUT DELETED.a INTO Test2(a)

答案1

得分: 1

你想要的语法是 DELETE <alias> OUTPUT ... FROM ...

DELETE T
OUTPUT deleted.a INTO dbo.Test2(a)
FROM dbo.Test T;

因此,对于你的 LEFT JOIN 示例,它将是:

DELETE t 
OUTPUT DELETED.a
INTO Test2(a)
FROM dbo.Test t 
     LEFT JOIN dbo.blah b on t.a = b.b --在简单的DELETE上进行LEFT JOIN似乎有点奇怪
WHERE t.c = 2;
英文:

The syntax you want is DELETE <alias> OUTPUT ... FROM ...:

DELETE T
OUTPUT deleted.a INTO dbo.Test2(a)
FROM dbo.Test T;

So, for your LEFT JOIN example, it would be:

DELETE t 
OUTPUT DELETED.a
INTO Test2(a)
FROM dbo.Test t 
     LEFT JOIN dbo.blah b on t.a = b.b --A lEFT JOIN on a simple DELETE seems odd mind
WHERE t.c = 2;

huangapple
  • 本文由 发表于 2023年2月6日 16:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定