Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Writing to a text file....

Author  Topic 

j_mact
Starting Member

17 Posts

Posted - 2003-06-05 : 10:53:16
Hi All,
I am really new to T-SQL and I need some help. Just want to know how to write to a text file on the server from a stored procedure. Any help would be greatly appreciated!!

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-05 : 11:14:29
look up bcp in bol

(Sorry: BULK COPY PROGRAM in Books Online)

Or DTS..Data Transformation Services...



Brett

8-)
Go to Top of Page

j_mact
Starting Member

17 Posts

Posted - 2003-06-05 : 11:25:51
Ok. Can you give me a link to where I can find the syntax for the
Bulk Copy Program?
Thanks.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-06-05 : 11:47:48
http://www.nigelrivett.net/WriteTextFile.html

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-05 : 11:49:06
Do you have a client install of sql server? also what version do you have..the client side installs should come with BOL

Try this on the net:

http://www.microsoft.com/sql/techinfo/productdoc/2000/default.asp



Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-06-05 : 11:52:20
Here's a sample sproc:


CREATE Procedure GetReportOutput_sp @Report_Name varchar(60)
,@Report_Type char(1)
,@Report_Procedure varchar(50)
,@User char(8)
as

Declare @Dest_Dir_Name varchar (255)
,@Dest_Drive_Letter char (3)
,@Dest_File_Name varchar (100)
,@TempTbl varchar (30)

--Note: Run Report Online.
IF @Report_Type = 'O'
Begin
Select @Report_Procedure = @Report_Procedure
Exec @Report_Procedure
Return
End

--Note: Run Report Batch
IF @Report_Type = 'B'
Begin
Select @Dest_Dir_Name = Dest_Dir_Name
,@Dest_Drive_Letter = Dest_Drive_Letter
,@Dest_File_Name = Dest_File_Name
From Report_Config
Where Report_Name = @Report_Name

Select @Report_Procedure = @Report_Procedure

Select @TempTbl = '##'+ltrim(rtrim(@User))+convert(varchar(8),getdate(),112)

Exec ( 'if exists (select * from tempdb..sysobjects where name = '+ ''''+@TempTbl+'''' + ')'
+ ' drop table ' + @TempTbl
)

Exec ( 'CREATE TABLE ' + @TempTbl + ' (ReportOutput varchar(8000))' )
Exec ( ' INSERT ' + @TempTbl + ' Exec ' + @Report_Procedure )

--* CREATE TABLE #ReportOutputTemp (ReportOutput varchar(8000))
--* INSERT #ReportOutputTemp Exec @Report_Procedure

Select @Dest_File_Name = ltrim(rtrim(@Dest_File_Name)) + ltrim(rtrim(@User)) + '-' + convert(varchar(8),getdate(),112) + '.csv'

EXEC (
'master..xp_cmdshell ' + ''''
+ 'bcp ' + @TempTbl + ' out '
+ @Dest_Drive_Letter + @Dest_Dir_name + @Dest_File_Name
-- + ' /c /r \n /UTaxUser /PTaxUser /S' + @@ServerName + ''''
+ ' /c /r \n /UTaxUser /Ptaxuser /S' + @@ServerName + ' /o' + @Dest_Drive_Letter + @Dest_Dir_name + 'Bcp.out'+''''
)

Exec ( 'DROP TABLE ' + @TempTbl )
End
GO




Brett

8-)
Go to Top of Page
   

- Advertisement -