?
Solved

tsql save variable to file

Posted on 2014-04-19
2
Medium Priority
?
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Assisted Solution

by:DcpKing
DcpKing earned 1000 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 1000 total points
ID: 40011323
I would use windows scripting host object via oacreate, like in this example.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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