SQL输出语法

时间:2013-06-07 13:21:11

标签: sql sql-server

虽然我认为这是一个相当简单的查询,但显然“输出'附近的语法不正确”。其他在线资源在调试此问题时没有帮助。

我在这里做错了什么?

DECLARE @changes TABLE (client_id_copy INT, client_id INT);

UPDATE gmdev.contacts 
SET client_id_copy=a.client_id
FROM gmdev.profile a, gmdev.contacts b
output client_id_copy, inserted.client_id into @changes
WHERE a.custid=b.custid
and NOT(Client_ID_copy > '')
and b.custid in
(select custid from gmdev.profile where custtype='EZ2');

修改

以下建议不起作用:

DECLARE @changes TABLE (client_id_copy INT, client_id INT);

UPDATE gmdev.contacts 
SET client_id_copy=a.client_id
OUTPUT client_id_copy, inserted.client_id into @changes
FROM gmdev.profile a, gmdev.contacts b
WHERE a.custid=b.custid
and NOT(Client_ID_copy > '')
and b.custid in
(select custid from gmdev.profile where custtype='EZ2');

4 个答案:

答案 0 :(得分:3)

DECLARE @changes TABLE (client_id_copy INT, client_id INT);

UPDATE gmdev.contacts 
SET client_id_copy=a.client_id
output inserted.client_id_copy, inserted.client_id into @changes
FROM gmdev.profile a, gmdev.contacts b
WHERE a.custid=b.custid
and NOT(Client_ID_copy > '') -- Weird...
and b.custid in
(select custid from gmdev.profile where custtype='EZ2');

答案 1 :(得分:3)

我们没有您的表格和数据,因此我们调试任何问题都有点棘手,但以下编译并运行:

create table contacts (client_id_copy int,custid int,client_id int)
create table profile(custid int,client_id int,custtype varchar(10))
DECLARE @changes TABLE (client_id_copy INT, client_id INT);

UPDATE contacts 
SET client_id_copy=a.client_id
OUTPUT deleted.client_id_copy,inserted.client_id into @changes
FROM profile a, contacts b
WHERE a.custid=b.custid
and NOT(Client_ID_copy > '')
and b.custid in
(select custid from profile where custtype='EZ2');

select * from @changes

正如我所说,我不知道它是否正确,因为我们不知道你的表是什么样的(我刚刚编写了一些定义)。 OUTPUT clause中列出的每个列都必须包含相关的表名称或别名(或inserteddeleted):

<column_name> ::=
   { DELETED | INSERTED | from_table_name } . { * | column_name }
   | $action

请注意,{ DELETED | INSERTED | from_table_name }未标记为可选,因此OUTPUT client_id_copy,不起作用。

答案 2 :(得分:1)

简化示例:

CREATE TABLE #contacts(client_id_copy INT NULL, custid INT NULL);
CREATE TABLE #profile(client_id INT NULL, custid INT NULL);

DECLARE @changes TABLE (client_id_copy INT, client_id INT);

UPDATE 
    #contacts 
SET 
    client_id_copy=a.client_id
OUTPUT 
    inserted.client_id_copy AS client_id_copy, 
    a.client_id AS client_id
    INTO @changes
FROM 
    #contacts AS b
    INNER JOIN #profile AS a ON
        a.custid=b.custid

DROP TABLE #contacts;
DROP TABLE #profile;

答案 3 :(得分:0)

在以下情况下,懒惰的系统管理员可能无法升级到最新版本的SQL

首先,请确保运行OUTPUT支持Select @@version;关键字。这将返回如下所示的单元格:

Microsoft SQL Server  2000 - 8.00.2282 (Intel X86) 
Dec 30 2008 02:22:41 
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.0 (Build 2195: Service Pack 4)

如果结果早于Microsoft SQL Server 2005 ,则不支持OUTPUT