Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Copying files using T-SQL


Copying files using T-SQL

Author
Message
Mark Markov
Mark Markov
SSC Rookie
SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)SSC Rookie (30 reputation)

Group: General Forum Members
Points: 30 Visits: 111

Hello,

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.


Carl Federl
Carl Federl
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2334 Visits: 4349
This can only be done by using xp_cmdshell. For privileges, this stored procedure can only be executed by someone with system administration rights.

P.S.
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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45002 Visits: 39880

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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search