Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Batch Variable in sqlcmd


Batch Variable in sqlcmd

Author
Message
ArjunaReddy
ArjunaReddy
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 554
Hi ,
I am writing a batch script which will take Full backup of the specified databases on our server. For this i am first creating a folder with the current date using OS command (mkdir %date:~-4%%date:~4,2%%date:~7,2%) with the intension that all the backups should go to this folder.

If i want to mention the current date folder (%date:~-4%%date:~4,2%%date:~7,2%) in the following sqlcmd how should i do?

sqlcmd -E -S MB3DS1022 -Q"BACKUP DATABASE [SFB10D02] TO DISK = N'D:\backups\\SFB10D02.bak' WITH NOFORMAT, NOINIT,NAME = N'SFB10D02-Full Database Backup',SKIP,NOREWIND, NOUNLOAD,STATS = 10"

Could anyone help me how can i achieve this?

Thanks,
Nagarjun.
matt6288
matt6288
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1208 Visits: 1350
bommaka.nagarjun (9/23/2009)
Hi ,
I am writing a batch script which will take Full backup of the specified databases on our server. For this i am first creating a folder with the current date using OS command (mkdir %date:~-4%%date:~4,2%%date:~7,2%) with the intension that all the backups should go to this folder.

If i want to mention the current date folder (%date:~-4%%date:~4,2%%date:~7,2%) in the following sqlcmd how should i do?

sqlcmd -E -S MB3DS1022 -Q"BACKUP DATABASE [SFB10D02] TO DISK = N'D:\backups\\SFB10D02.bak' WITH NOFORMAT, NOINIT,NAME = N'SFB10D02-Full Database Backup',SKIP,NOREWIND, NOUNLOAD,STATS = 10"

Could anyone help me how can i achieve this?

Thanks,
Nagarjun.




If you are doing all of this in the batch file then you can set a variable like this.

set folder=%date:~-4%%date:~4,2%%date:~7,2%


You can then reference this variable in sqlcmd line like this

sqlcmd -E -S MB3DS1022 -Q"BACKUP DATABASE [SFB10D02] TO DISK = N'D:\backups\%folder%\SFB10D02.bak' WITH NOFORMAT, NOINIT,NAME = N'SFB10D02-Full Database Backup',SKIP,NOREWIND, NOUNLOAD,STATS = 10"


I think that should work but you might want to echo the line above just to make sure
yazalpizar
yazalpizar
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 5
I was looking for something like this, but instead of the folder, I want to create a backup of the database each time with different name, could be DDMMYYYYHHMM.bak Can I use the same solution?
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