Copying files using T-SQL

  • 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.

     

  • 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

  • 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.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply