数据导出到文本文本文件里。
字段 字段2
tt a
tt b
pp s
pp g
pp f
pp j
导出来的文本里的格式是:
tt a,b
pp s,g,f,j
------解决方案--------------------Create Table TEST
(A Varchar(10),
B Varchar(10))
Insert TEST Select 'tt ', 'a '
Union All Select 'tt ', 'b '
Union All Select 'pp ', 's '
Union All Select 'pp ', 'g '
Union All Select 'pp ', 'f '
Union All Select 'pp ', 'j '
GO
Create Function F_TEST(@A Varchar(10))
Returns Varchar(8000)
As
Begin
Declare @S Varchar(8000)
Select @S = ' '
Select @S= @S + ', ' + B From TEST Where A = @A
Select @S = Stuff(@S, 1, 1, ' ')
Return @S
End
GO
Declare @S Varchar(8000)
Set @S= 'bcp "Select A, TEST.dbo.F_TEST(A) As B from TEST.dbo.TEST Group By A " Queryout "D:\TEST.txt " -c -q -S "(local) " -U "sa " -P "sa " '
Exec master..xp_cmdshell @S
GO
Drop Table TEST
Drop Function F_TEST