2010-06-23 147 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