1

我有一个包含一个字段(信息)的表。

Information
===========
Hello World 1
This is testing message
How are  you

我想创建三个文本文件(行数为 3),它们的内容基于行中的值。

所以,

  • File1.txt 将有Hello World 1
  • File2.txt 将有This is testing message

我们如何在 SQL Server 中实现这一点?

4

1 回答 1

0

尝试这样的事情: -

use master
go

declare @DSQL Nvarchar(max)
declare @counter int
declare @maxrows int
declare @filename Nvarchar(30)

select @counter=1, @maxrows = 0

create table t1 (
 sno int identity(1,1) not null,
 filename varchar(5),
 filecontent varchar(100)
)

insert into t1
select 'FN1', 'Hello'
UNION
select 'FN2', 'Good Morning'
UNION
select 'FN3', 'How are you?'
UNION
select 'FN14', 'Where are you?'

select @maxrows = count(*) from t1

--SELECT * FROM T1

while (@counter <= @maxrows)
begin
  select @filename = filename from t1
   where sno = @counter
select @DSQL = N'exec xp_cmdshell' + ' ''bcp "select filecontent from master.dbo.T1 where sno = ' + cast(@counter as nvarchar(10)) + '" queryout "d:\temp\' + @filename + '.txt" -T -c -S home-e93994b54f'''

print @dsql
exec sp_executesql @DSQL
   select @counter = @counter + 1
end

drop table t1
于 2012-12-05T17:14:57.493 回答