2010-06-23 6 views
0

아래 스크립트는 the question i posted last nigh t에 대한 회원 (OMG Ponies)이 작성했습니다. 스크립트에 구문 오류가있어서 알아 내지 못했습니다. 누군가 그것을 시험해 볼 수 있습니까? 그것은 MS SQL 서버 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 ------------------------- 의 구문이 잘못되었습니다 ' 고르다'. 메시지 : 156, 수준 : 15, 상태 : 1, 절차 :, 줄 : 5

[오류] 스크립트 줄 : 1-10 ------------------ ------- ')'근처에 구문이 잘못되었습니다. 메시지 : 102, 수준 : 15, 상태 : 1, 절차 :, 줄 : 9

+0

귀하의 계정에는 다른 질문이 없습니다. 링크를 제공 할 수 있습니까? –

+0

신경 쓰지 마라 : 찾았 어 : http://stackoverflow.com/questions/3098201/correcting-the-sql-syntax –

+0

그 스크립트는 SQL 2000에서 작동하지 않을 것이다. –

답변

0

이 작동합니다!

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

관련 문제