Syntaxe de sortie SQL

Bien que je considère qu'il s'agit d'une requête assez simple, il y a apparemment "Syntaxe incorrecte près de" sortie "". D'autres ressources en ligne n'ont pas été utiles pour le debugging de ce problème.

Qu'est-ce que je fais mal ici?

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'); 

Modifier:

La suggestion suivante NE FONCTIONNE PAS:

 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'); 

 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'); 

Nous n'avons pas vos tables et données, donc c'est un peu difficile pour nous de déboguer tous les problèmes, mais ce qui suit comstack et fonctionne:

 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 

Comme je le dis bien, je ne sais pas si c'est correct car nous ne soaps pas à quoi ressemblent vos arrays (je viens de faire quelques définitions). Chaque colonne répertoriée dans la clause OUTPUT doit inclure le nom ou l'alias de la table pertinente (ou inserted ou deleted ):

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

Et notez que { DELETED | INSERTED | from_table_name } { DELETED | INSERTED | from_table_name } { DELETED | INSERTED | from_table_name } n'est pas marqué comme optionnel, c'est pourquoi OUTPUT client_id_copy, ne fonctionne pas.

Un exemple simplifié:

 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; 

Dans certains cas, les administrateurs système paresseux peuvent ne pas mettre à niveau vers une version à jour de SQL .

Tout d'abord, assurez-vous que le mot key OUTPUT est supporté en exécutant Select @@version; Cela returnnera une cellule comme ça:

 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) 

Si le résultat est plus ancien que Microsoft SQL Server 2005 alors OUTPUT n'est pas supporté!