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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Alternative to .xp_cmdshell

Author  Topic 

JackOfAllTrades
Starting Member

12 Posts

Posted - 2009-04-14 : 15:45:51
I've got this working. Dumps the content of the column to a txt file. Thinking about security and having xp_cmdshell enabled, what are my alternatives.

I'm using this in a scheduled job that dumps the data to file so I can process that data via batch file from a different server. If there's a means to do this all in one batch file, then can I do that without enabling xp_cmdshell?

EXEC master..xp_cmdshell 'bcp "select OneColumn from DBname.dbo.TestTable" QUERYOUT c:\test.txt -c -S SQLtest -T'
Go


-Steve


Colt's, Ruger's, Dan Wesson, & Kimber are my friends!
Proud to be a U.S. Navy Veteran.

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2009-04-14 : 15:52:44
Another viable option is CLR. You can create an assembly to perform this work for you.
Go to Top of Page

JackOfAllTrades
Starting Member

12 Posts

Posted - 2009-04-14 : 19:18:25
CLR?

-Steve
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-14 : 19:47:19
You could also do the work via SSIS, which would only require permissions to the table.

CLR allows us to run .NET code inside SQL Server. CLR would be just as unsafe as xp_cmdshell though as in order to get to the file system, you have to grant elevated permissions on the database.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-04-14 : 20:43:43
Another option is to put the bcp command in a SQL Agent job as an Operating System task.
Go to Top of Page

JackOfAllTrades
Starting Member

12 Posts

Posted - 2009-04-15 : 01:50:14
that's actually my intent, to put this in an Agent job and schedule it. If I do that, can I turn off xp_cmdshell?

-Steve

Colt's, Ruger's, Dan Wesson, & Kimber are my friends!
Proud to be a U.S. Navy Veteran.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-04-15 : 06:52:22
If you want to, sure. You can run it any time using sp_start_job.
Go to Top of Page

tfountain
Constraint Violating Yak Guru

491 Posts

Posted - 2009-04-16 : 15:37:11
quote:
Originally posted by tkizer

CLR would be just as unsafe as xp_cmdshell though as in order to get to the file system, you have to grant elevated permissions on the database.


I think comparing an assembly with a permission set of EXTERNAL_ACCESS or UNSAFE to elevated permissions required for executing xp_cmdshell is a bit like comparing apples and oranges.

Just to execute xp_cmdshell requires elevated permissions. Once you have permission to execute xp_cmdshell there is nothing preventing any command to be ran.

The difference with CLR is the code to execute the file system needs to be written. Once written there are elevated permissions required to import assemblies that use EXTERNAL_ACCESS or UNSAFE permission sets (but not SAFE). However, once the assembly is created, all the permissions required to execute them do not require elevated permissions - just the typical permissions you would grant to execute objects (functions, procs, etc). And even if you can execute a CLR object, that does not imply you can execute anything through it... it will only do what the .NET code was written to do.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-16 : 15:51:14
My point is that the database setting remains in place in order for the CLR object to access the file system.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -