Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output to text

Status
Not open for further replies.

be1s

Programmer
Dec 8, 2003
59
US
I'd like to take the results of a stored proc and place into a text file. I don't want a straight export. The data that gets placed into the text file will be based on some conditions and formatted in a certain way. Any way to write to a text file from a stored procedure?
 
is this of any help:
Code:
-- Name: Writing to Text File
-- Description:With this Stored procedur
--     e you can log messages to a text file
-- By: Anees Ahmed
--
-- Inputs:@text VARCHAR(100)=what is the
--      message you want to enter
--@file VARCHAR(100)=what IS the file name
--@overwrite BIT = 0
--


CREATE PROC writing_2_file
@text VARCHAR(100),
@file VARCHAR(100),
@overwrite BIT = 0
AS


    BEGIN
    	SET NOCOUNT ON
    	DECLARE @query VARCHAR(255)
    	SET @query = RTRIM('echo ' + COALESCE(LTRIM(@text),'-') + CASE WHEN (@overwrite = 1) THEN ' > ' ELSE ' >> ' END + RTRIM(@file))
    	EXEC master..xp_cmdshell @query
    	PRINT @query
    	SET NOCOUNT OFF
END
GO

--exec writing_2_file 'There is already an object named writing_2_file in the database', 'c:\test.txt', 1

-- 0 appends and 1 overwrites

or this
Code:
[bandito] [blue]DBomrrsm[/blue] [bandito]
 
If you actually want to export a table or portion thereof, then read up on DTS packages and exporting files in BOL.
-Karl

[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top