Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 writing to text file from stored procedure
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

38 Posts

Posted - 09/26/2013 :  10:47:01  Show Profile  Reply with Quote
Want to write from a table variable to a text file from a stored procedure.
Read about xp_cmdshell bcp etc. but worried because it's supposed to be a security problem and needs to be from a permanent database.
Also am getting error "The EXECUTE permission was denied on the object 'xp_cmdshell'..."

1. Is xp_cmdshell a bad idea to use even if I get permissions ?
2. Can a "permanent" table be used in a stored procedure starting out fresh each time with 0 rows rather than use a table variable ?

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 09/26/2013 :  11:04:02  Show Profile  Reply with Quote
You have to enable xp_cmdshell. By default it is turned off. See here for script to turn it on:

If you must write from stored procedure to a file, xp_cmdshell is probably the easiest option. You could use CLR stored procedures with external access instead.

You can use a permanent table or table variable to query from for getting your data. That should not make any difference as far as security goes.
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 09/28/2013 :  03:51:00  Show Profile  Reply with Quote
You may also use OPENROWSET to do data export to text file


Another way to do it to use SSIS /Export import wizard to create a package to do the data export to text file and call it from sql server job. Then in your sp use sp_start_job to launch the job and do data export.

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000