Solved

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

Posted on 2013-01-28
4
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

695 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