Solved

tsql save variable to file

Posted on 2014-04-19
2
225 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 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 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