Solved

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

Posted on 2013-01-28
4
175 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 40

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Table where row act as column 11 67
SQL convert varchar to INT 17 29
sql query 5 38
SQL Syntax 6 36
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

685 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