Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

tsql save variable to file

Hello,

SQL Server 2008.

I have a stored procedure that returns an xml document.

I would like to save the variable to a file.  It can be very large.

Can this be done in script?

This is how i run my sp:

DECLARE	@return_value int

EXEC	@return_value = [dbo].[SKL_XML_Export_InitialLoad]
		@StartDate = '2010-01-01',
		@EndDate = '2010-02-01'

SELECT	'Return Value' = @return_value

Open in new window

0
soozh
Asked:
soozh
2 Solutions
 
DcpKingCommented:
Probably the easiest way to do this is to make an export job out of it using the SSIS built in to your SQL Server.

Before you do this, run your procedure and make sure that the output has a name (in the header row of the results) - see my attachment "Sample output from procedure". Alter the procedure to make sure that the output is named if necessary.
At this point also create a blank Text File in the folder you want to use, using Windows Explorer

Right-click on your database entry in SSMS, select Tasks and Export Data to start the wizard.
Data Source is your database.
Data Destination should be a flat text file, so drop down Destination and select Flat File Destination. Enter the name of the Text File you just created for File Name. For format I used Fixed Width.
Specify Table or Query. Choose Write a Query.
Provide Source Query. Enter your procedure call:
EXEC	[dbo].[SKL_XML_Export_InitialLoad]  '2010-01-01', '2010-02-01'

Open in new window

Configure Destination. Definitely click on Edit Mappings and see what the wizard has achieved. See my Column Mappings screengrab.
Save and Run. Save your package out in the file system (remember where!)

Click on and run your package. Check that the text file actually has what you want it to have.

Then load your package into BIDS, deploy it to your server, and run it whenever you like. If you want to start it from some other job or a stored procedure, use the built-in procedure sp_start_job.

hth

Mike
Screenshot-from-2014-04-20-08-02.png
Screenshot-from-2014-04-20-08-12.png
0
 
Vadim RappCommented:
I would use windows scripting host object via oacreate, like in this example.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now