[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

tsql save variable to file

Posted on 2014-04-19
2
Medium Priority
?
241 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

My client sends a request to me that they want me to load data, which will be returned by Web Service APIs, and do some transformation before importing to database. In this article, I will provide an approach to load data with Web Service Task and X…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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