2010-06-23 5 views
0

Le script ci-dessous a été écrit par un membre (OMG Ponies) pour the question i posted last nigh t. Il y avait une erreur de syntaxe dans le script, je ne pouvais pas le comprendre. Quelqu'un peut-il essayer? Il a utilisé MS SQL Server 2000/2005/2008Fusion de deux tables en une seule table

select 
'Steve'  as name, 
'4/20/1960' as DOB, 
'12456'  as agentID, 
'Smith'  as agentName 
into #TABLE1 

insert into #TABLE1 
select 
'Steve'  as name, 
'4/20/1960' as DOB, 
'12456'  as agentID, 
'John'   as agentName 

insert into #TABLE1 
select 
'Steve'  as name, 
'4/20/1960' as DOB, 
'12456'  as agentID, 
'Lary'   as agentName 

select * from #TABLE1 

.

 
+---------+-----------+----------+------------------+ 
| Name | DOB  | AgentID | AgentName  | 
+---------+-----------+----------+------------------+ 
| Steve | 4/20/1960 | 12456  | John   | 
+---------+-----------+----------+------------------+ 
| Steve | 4/20/1960 | 12456  | Lary   | 
+---------+-----------+----------+------------------+ 
| Steve | 4/20/1960 | 12456  | Smith   | 
+---------+-----------+----------+------------------+ 


+---------+-----------+----------+----------------------+ 
| Name | DOB  | AgentID | AgentName  | 
+---------+-----------+----------+----------------------+ 
| Steve | 4/20/1960 | 4444  | John,Larry, Smith | 
+---------+-----------+----------+----------------------+ 

.

SELECT DISTINCT 
     t.name, 
     t.dob, 
     t.agentid, 
     STUFF(ISNULL(SELECT ', ' + x.agentname 
         FROM TABLE1 x 
        WHERE x.agentid = t.agentid 
        GROUP BY x.agentname 
        FOR XML PATH ('')), ''), 1, 2, '') 
    FROM TABLE1 t 

[Erreur] lignes de script: 1-10 ------------------------- syntaxe incorrecte près du mot-clé ' SÉLECTIONNER'. Msg: 156, Niveau: 15, State: 1, Procédure:, Ligne: 5

[Erreur] Lignes de script: 1-10 ------------------ ------- Syntaxe incorrecte près de ')'. Msg: 102, Niveau: 15, L'état: 1, La procédure:, Ligne: 9

+0

Il n'y a pas d'autres questions sur votre compte. Pouvez-vous fournir un lien? –

+0

Peu importe: trouvé: http://stackoverflow.com/questions/3098201/correcting-the-sql-syntax –

+0

Ce script ne fonctionnera pas dans SQL 2000. Est-ce que c'est ce que vous utilisez? –

Répondre

0

Cela fonctionne!

select 'Steve' as name, '4/20/1960' as DOB, '12456' as agentID, 'Smith' as agentName into #TABLE1 

insert into #TABLE1 select 'Steve' as name, '4/20/1960' as DOB, '12456' as agentID, 'John' as agentName 

insert into #TABLE1 select 'Steve' as name, '4/20/1960' as DOB, '12456' as agentID, 'Lary' as agentName 

select * from #TABLE1 

SELECT DISTINCT t.name, t.dob, t.agentid, STUFF(ISNULL((SELECT ', ' + x.agentname FROM #TABLE1 x WHERE x.agentid = t.agentid GROUP BY x.agentname FOR XML PATH ('')), ''), 1, 2, '') FROM #TABLE1 t 
DROP TABLE #Table1 

--+---------+-----------+----------+------------------+ | Name | DOB | AgentID | AgentName | +---------+-----------+----------+------------------+ | Steve | 4/20/1960 | 12456 | John | 
--+---------+-----------+----------+------------------+ | Steve | 4/20/1960 | 12456 | Lary | +---------+-----------+----------+------------------+ | Steve | 4/20/1960 | 12456 | Smith | +---------+-----------+----------+------------------+ 

--+---------+-----------+----------+----------------------+ | Name | DOB | AgentID | AgentName | +---------+-----------+----------+----------------------+ | Steve | 4/20/1960 | 4444 | John,Larry, Smith | +---------+-----------+----------+----------------------+ 
+1

Ce code manquait un ')', puis les alias manquaient le # – Baaju

Questions connexes