Solved

tsql save variable to file

Posted on 2014-04-19
2
207 Views
Last Modified: 2016-02-10
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
Comment
Question by:soozh
2 Comments
 
LVL 16

Assisted Solution

by:DcpKing
DcpKing earned 250 total points
ID: 40011288
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
 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 250 total points
ID: 40011323
I would use windows scripting host object via oacreate, like in this example.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Lessons learned during ten years of interviewing for SQL Server Integration Services (SSIS) and other Extract-Transform-Load (ETL) contract roles and two years of staff manager interviewing contractors.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now