Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL Script To Copy text file from one location to other and Delete the File after Copying tO some log folder Expand / Collapse
Author
Message
Posted Tuesday, July 24, 2012 5:49 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:30 AM
Points: 31, Visits: 119
Hi ,
I need suggestions for following :

I am trying to copy a text file from One location to other location and After copying file Need to Delete file from source location and need to keep a log of this file
Post #1334398
Posted Tuesday, July 24, 2012 7:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:30 AM
Points: 31, Visits: 119
I got the solution from one of my colleagues

Please find the solution below

DECLARE @from VARCHAR(200) 
DECLARE @to VARCHAR(200)
DECLARE @to_move VARCHAR(200)
DECLARE @sql VARCHAR(200)
DECLARE @sql_mov VARCHAR(200)
DECLARE @sql_del VARCHAR(200)

SELECT @to = '\\X\20120724.txt',
@from = '\\Y\20120724.txt',
@to_move = '\\Z\20120724.txt'

SET @sql = 'copy ' + @from + ' ' + @to
SET @sql_mov = 'copy ' + @from + ' ' + @to_move
SET @sql_del = 'del ' + @from

EXECUTE master..xp_cmdshell @sql

EXECUTE master..xp_cmdshell @sql_mov

EXECUTE master..xp_cmdshell @sql_del

Post #1334433
Posted Tuesday, July 24, 2012 7:14 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 2:11 PM
Points: 35,772, Visits: 32,441
Rakesh.Chaudhary (7/24/2012)
I got the solution from one of my colleagues

Please find the solution below

DECLARE @from VARCHAR(200) 
DECLARE @to VARCHAR(200)
DECLARE @to_move VARCHAR(200)
DECLARE @sql VARCHAR(200)
DECLARE @sql_mov VARCHAR(200)
DECLARE @sql_del VARCHAR(200)

SELECT @to = '\\X\20120724.txt',
@from = '\\Y\20120724.txt',
@to_move = '\\Z\20120724.txt'

SET @sql = 'copy ' + @from + ' ' + @to
SET @sql_mov = 'copy ' + @from + ' ' + @to_move
SET @sql_del = 'del ' + @from

EXECUTE master..xp_cmdshell @sql

EXECUTE master..xp_cmdshell @sql_mov

EXECUTE master..xp_cmdshell @sql_del



If you're going to use xp_CmdShell for this, there is a MOVE command in DOS and in ROBOCOPY.


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

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1334447
Posted Tuesday, July 24, 2012 8:12 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:30 AM
Points: 31, Visits: 119
Thanks for the information about MOVE command. I was not aware about this earlier.
I have now implemented my requirement using the MOVE command.
Is there any other way to implement this other than using the xp_cmshell option?
Post #1334510
Posted Tuesday, July 24, 2012 8:41 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 9:20 AM
Points: 1,372, Visits: 1,567
Other option is to explore SSIS..

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1334541
Posted Wednesday, July 25, 2012 4:42 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:30 AM
Points: 31, Visits: 119
Thanks!
What should I use ,If want to Overwrite the existing file :

Currently I am using below query ,but its not working....

@sql = 'copy ' + @from + ' ' + @to +' /Y'
Post #1335026
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse