Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-28
4
Medium Priority
?
179 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 750 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

609 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