I am using SQL 8.0 and have the challenge of writing text files from a table.
My table contains three fields, file name, file data, and an index. I need to
create a file for every row that uses the 1st field as the file name, which
is already in the correct format [filename.txt] and I need the value from the
second column, filed data, to be the text within the text file.
Can anyone assit me in creating these files? I cannot find anything to
assist me with this in the online books nor MSDN.
You can use this demo code as a start:
-- OJ: TEXTCOPY example
-- Loading files into db &
-- exporting files out to folder
--
--TEXTCOPY IN
--create tb to hold data
create table tmp(fname varchar(100),img image default '0x0')
go
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50),
@.user sysname,
@.pass sysname
set @.user='myuser'
set @.pass='mypass'
--specify desired folder
set @.path='c:\winnt\'
set @.sql='dir ' + @.path + '*.bmp /c /b'
--insert filenames into tb
insert tmp(fname)
exec master..xp_cmdshell @.sql
--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp
open cc
fetch next from cc into @.fname
while @.@.fetch_status=0
begin
set @.sql='textcopy /s"'+@.@.servername+'" /u"'+@.user+'" /p"'+@.pass+'"
/d"'+db_name()+'" /t"tmp" /c"img" /w"where fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /i' + ' /z'
print @.sql
exec master..xp_cmdshell @.sql ,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc
go
select * from tmp
go
--TEXTCOPY OUT
declare @.sql varchar(255),
@.fname varchar(100),
@.path varchar(50),
@.user sysname,
@.pass sysname
set @.user='myuser'
set @.pass='mypass,'
--specify desired output folder
set @.path='c:\tmp\'
set @.sql='md ' + @.path
--create output folder
exec master..xp_cmdshell @.sql
--loop through and insert file contents into tb
declare cc cursor
for select fname from tmp
open cc
fetch next from cc into @.fname
while @.@.fetch_status=0
begin
set @.sql='textcopy /s"'+@.@.servername+'" /u"'+@.user+'" /p"'+@.pass+'"
/d"'+db_name()+'" /t"tmp" /c"img" /w"where fname=''' + @.fname + '''"'
set @.sql=@.sql + ' /f"' + @.path + @.fname + '" /o' + ' /z'
print @.sql
exec master..xp_cmdshell @.sql ,no_output
fetch next from cc into @.fname
end
close cc
deallocate cc
set @.sql='dir ' + @.path + '*.bmp /c /b'
exec master..xp_cmdshell @.sql
go
drop table tmp
go
"Dorian Foster" <DorianFoster@.discussions.microsoft.com> wrote in message
news:94D63419-1F96-4437-A07E-C293141D84C8@.microsoft.com...
> I am using SQL 8.0 and have the challenge of writing text files from a
table.
> My table contains three fields, file name, file data, and an index. I need
to
> create a file for every row that uses the 1st field as the file name,
which
> is already in the correct format [filename.txt] and I need the value from
the
> second column, filed data, to be the text within the text file.
> Can anyone assit me in creating these files? I cannot find anything to
> assist me with this in the online books nor MSDN.
|||Refer to Books Online; search for "Write File Transformation"
"Dorian Foster" wrote:
> I am using SQL 8.0 and have the challenge of writing text files from a table.
> My table contains three fields, file name, file data, and an index. I need to
> create a file for every row that uses the 1st field as the file name, which
> is already in the correct format [filename.txt] and I need the value from the
> second column, filed data, to be the text within the text file.
> Can anyone assit me in creating these files? I cannot find anything to
> assist me with this in the online books nor MSDN.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment