Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Copying files using T-SQL Expand / Collapse
Posted Wednesday, December 21, 2005 4:35 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 30, 2008 6:34 AM
Points: 30, Visits: 111


Is there ANY way to copy files from within T-SQL other than using xp_cmdshell in the master table?

I am trying to launch a batch file using xp_cmdshell, for example:

declare @command varchar(200)
set @command = 'call c:\temp\test.bat'
exec master..xp_cmdshell @command

and it doesn't work... I get an error:

xpsql.cpp: Error 997 from GetProxyAccount on line 472

I simply need to copy some files from one location to another but it needs to be from within a T-SQL script and cannot be a Copy Files task in a DTS package =(

Thank you.


Post #246032
Posted Wednesday, December 21, 2005 5:09 PM


Group: General Forum Members
Last Login: Thursday, November 24, 2016 7:36 AM
Points: 2,316, Visits: 4,349
This can only be done by using xp_cmdshell. For privileges, this stored procedure can only be executed by someone with system administration rights.

I recently read Joe Celko's SQL book and he has the following story:

Forum Question: What kind of rock is best for hammering in screws?
Answer: Granite

This is the same type of question and the appropriate answer is you do not copy file from SQL Server, you use an application.

SQL = Scarcely Qualifies as a Language
Post #246039
Posted Thursday, December 22, 2005 12:41 AM



Group: General Forum Members
Last Login: Today @ 7:14 AM
Points: 42,072, Visits: 39,455

Carl, sometimes there's just no application to be had... SQL is it.

Mark, the reason you are getting the error is that you must either have SA permissions or your DBA is going to have to setup a special proxy account for you.  The hard part will be both convincing the DBA that you gotta have it to do your job and then them figuring out how to do it.

Now, you could cheat a little bit... when you meet an immovable object, go around it... run your SQL through a batch file that uses OSQL... it'll take a lot more finagling with outputing results sets to files and the like, not to mention some pretty good ol' fashioned DOS knowledge, but it can be done.

I just went through the very same thing with my DBA's... the big concern with xp_CmdShell is that it's very powerful and you can do a lot of damage to the server either maliciously or by accident.  It also makes for one hell of a security hole unless you have DBA's that know how to handle the tool.  Fortunately, I have good DBA's that said, "you develop it on your desktop server and how to move it to production.  We'll figure out how to run it for you."  What a bunch of great folks... they setup a special proxy where I can call a special SQL job to run my code by proxy.  Because of other security measures they took, if I change the store proc, I can't apply grants to the proc so that even I can't run it if it's been changed.  The DBA's have to approve all changes and they're the only ones that can apply the correct grants.  And, no one but the special account I log into can run the proc.  It's amazing how they've given me the flexibility to run it anytime I need to and have locked it down so well.  Like I said, great DBA's.

And to heck with rocks, use a hammer 'cause to a hammer, everything is a nail

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

Helpful Links:
How to post code problems
How to post performance problems
Post #246088
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse