4

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

我在这里做错了什么?

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

4 回答 4

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子句中列出的每一列都必须包含相关的表名或别名(或inserteddeleted):

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

请注意,{ DELETED | INSERTED | from_table_name }它没有标记为可选,所以这就是为什么OUTPUT client_id_copy,不起作用。

于 2013-06-07T14:20:23.193 回答
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');
于 2013-06-07T13:22:37.520 回答
1

A simplified example:

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;
于 2013-06-07T14:29:32.353 回答
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支持!

于 2013-06-07T14:38:21.317 回答