2010-06-23 189 views
0

以下腳本是由成員(OMG Ponies)爲the question i posted last nigh t寫的。 腳本中有語法錯誤,我找不出來。有人可以試試嗎?它使用MS SQL Server 2000/2005/2008將兩個表合併成一張表

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 

[錯誤]腳本行:關鍵字鄰近1-10 ------------------------- 語法不正確'選擇'。 Msg:156,Level:15,State:1,Procedure:,Line:5

[Error] Script lines:1-10 ------------------ ------- ')'附近語法不正確。 信息:102,級別:15,狀態:1,程序:,行:9

+0

您的帳戶沒有其他問題。你能提供一個鏈接嗎? – 2010-06-23 14:18:56

+0

沒關係:發現它:http://stackoverflow.com/questions/3098201/correcting-the-sql-syntax – 2010-06-23 14:19:33

+0

該腳本不會在SQL 2000中工作。那是你在用什麼? – 2010-06-23 14:21:48

回答

0

This Works!

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

此代碼缺少')',然後別名缺少# – Baaju 2010-06-23 14:23:16