SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Script To Copy text file from one location to other and Delete the File after Copying tO some...


SQL Script To Copy text file from one location to other and Delete the File after Copying tO some log folder

Author
Message
Rakesh.Chaudhary
Rakesh.Chaudhary
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 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
Rakesh.Chaudhary
Rakesh.Chaudhary
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 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


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90766 Visits: 41150
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Rakesh.Chaudhary
Rakesh.Chaudhary
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 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?
Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2134 Visits: 1599
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


Rakesh.Chaudhary
Rakesh.Chaudhary
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 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'
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search