Solved

Is it possible to Copy files from one server to pc using SQL

Posted on 2013-01-28
4
170 Views
Last Modified: 2013-03-12
I am running this batch to copy files, but I would like to perform the same function in SQL.  

Is it possible?  if yes, full credit will be given to expert providing fule TSQL Syntax.

The code below gets the first 3 characters of a file and creates a folder, then copies the files to it's destination.  We have four hospitals and we are sending the files to each hospitals pharmacy pc

@echo off
setlocal enabledelayedexpansion
set Source=C:\Temp\EMARTEST
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[BACKUPDATA]"') do set DataStart=%%a
set /a TypeCount = 0
for /f "tokens=1* skip=%DataStart% delims= " %%a in ('type "%~f0"') do (
	set /a TypeCount += 1
	set SourceType[!TypeCount!]=%%a
	set Target[!TypeCount!]=%%b
)
for /l %%i in (1, 1, %TypeCount%) do (
	ECHO robocopy.exe "%Source%" "!Target[%%i]!" !SourceType[%%i]! /mov /r:2 /w:1
)

goto :eof
[BACKUPDATA]
MHG*.* \\10.X.X.20\emarbackup$
ELA*.* \\10.X.X.15\emarbackup$
CHH*.* \\10.X.X.30\emarbackup$
CPH*.* \\10.X.X.35\emarbackup$ 

Open in new window

0
Comment
Question by:epicazo
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 38827955
You can save the batch file on the sql server and call the batch in sql srever using xp_cmdShell


TSQL Statement

exec master..xp_cmdShell 'c:\test.bat'

One thing to note, by default xp_cmdShell is disabled, you should enable it in order to run this
0
 

Author Comment

by:epicazo
ID: 38828295
the problems is that I wont be able to monitor whether the files successfully or not.
0
 
LVL 39

Accepted Solution

by:
lcohan earned 250 total points
ID: 38828315
You can run the copy itself from SQL command not from the batch like


exec master..xp_cmdShell 'copy c:\test.bat \\new_box\my_folder\'

and you will get the results like:

"The system cannot find the file specified."

You can use even robocopy if it's from windows OS with results like below:

output
NULL
-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------
NULL
  Started : Mon Jan 28 14:50:45 2013
NULL
2013/01/28 14:50:48 ERROR 53 (0x00000035) Getting File System Type of Destination \\new_box\my_folder\
The network path was not found.
NULL
   Source : c:\test.bat\
     Dest - \\new_box\my_folder\
NULL
    Files : *.*
     
  Options : *.* /COPY:DAT /R:1000000 /W:30
NULL
------------------------------------------------------------------------------
NULL
2013/01/28 14:50:48 ERROR 2 (0x00000002) Accessing Source Directory c:\test.bat\
The system cannot find the file specified.
NULL
0
 

Author Closing Comment

by:epicazo
ID: 38978649
I wanted full tsql only and prevent batch, but I could use this for now.

thanks
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now