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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 writing to text file from stored procedure

Author  Topic 

bill_
Starting Member

38 Posts

Posted - 2013-09-26 : 10:47:01
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
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-26 : 11:04:02
You have to enable xp_cmdshell. By default it is turned off. See here for script to turn it on: http://technet.microsoft.com/en-us/library/ms190693.aspx

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-28 : 03:51:00
You may also use OPENROWSET to do data export to text file


see


http://www.databasejournal.com/scripts/article.php/3587101/Load-TXT-File-using-OPENROWSET-function.htm

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
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -