Solved

sqlcmd add timestamp to output file

Posted on 2013-06-12
4
2,214 Views
Last Modified: 2013-06-17
Hello,
  I have code:
::TIME
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
::Darom diff backup
echo .....Runing DIFF Backup.....Do not close this window....
echo .......
SQLCMD -E -S %SERVERNAME% -Q "BACKUP DATABASE [%DATABASENAME%] TO DISK='%BACKUPDIR%%DATABASENAME%_DIFF_MANUAL_%mytime%-%mydate%.bak' WITH DIFFERENTIAL,INIT, COMPRESSION, STATS = 10"

Open in new window


Now i am using time and date from cmd, but how can i use sqlcmd time stamp to add date to backup file. if i run backup from sql server i can add
"+ REPLACE(convert(nvarchar(20),GetDate(),120),':','-')" and it will work.

Is it possible to use same thing on sqlcmd
0
Comment
Question by:thinkbot
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39242267
Batch files make my head hurt...  How about some powershell?!

$databaseName = "testing";
$serverName = "servar";
$backupDirPath = "C:\testing\path\";

$currDate = get-date -format MMddyyyy_hhmmss;
$fullPath = $backupDirPath + $databaseName + "_DIFF_MANUAL_" + $currDate + ".bak";
$expression = "SQLCMD -E -S " + $serverName + " -Q `"BACKUP DATABASE " + $databaseName + " TO DISK='" + $fullPath + "' WITH DIFFERENTIAL,INIT, COMPRESSION, STATS = 10`"";

echo $expression;

#Invoke-Expression $expression;

Open in new window


That, in it's current form is only going to echo the command to use in the invoke-expression cmdlet, but I figure you should probably look at it first before you run it.  You would just remove the comment in front of the invoke-expression cmdlet.
0
 

Author Comment

by:thinkbot
ID: 39243760
i think i asked in wrong way. Lets try again. Is it possible to use sql function  getdate() in sqlcmd ?

i need to make name to be same as from sql server backup:
database__2013_06_08_010000_8411342.bak

First part is easy "2013_06_08_010000", date and time, but how to add "_8411342" this number.

Thanks,
  Tadas
0
 
LVL 8

Accepted Solution

by:
didnthaveaname earned 500 total points
ID: 39248722
The answer is yes, but I think the problem lies in the fact that even out of SSMS, you would need to do this differently.  You can't concatenate like this:

BACKUP DATABASE <<dbname>> TO DISK=N'<<PATH HERE>>' + REPLACE( convert( nvarchar( 20 ),GetDate(),120), ':', '-') + N'.bak' WITH DIFFERENTIAL, INIT, COMPRESSION, STATS = 10;

Open in new window


You would need to do something like this:
declare @fileName nvarchar( 200 ) = N'<<PATH_HERE>\<<DBNAME>>_DIFF_MANUAL_' + REPLACE(convert(nvarchar(20),GetDate(),120),':','-') + N'.bak';
BACKUP DATABASE <<dbname>> TO DISK=@fileName WITH DIFFERENTIAL, INIT, COMPRESSION, STATS = 10;

Open in new window


Which you can absolutely plop into a SQLCMD:
set servername=<<serverName>>
set databasename=<<fileName>>
set backupdir=<<path>>

::Darom diff backup
echo .....Runing DIFF Backup.....Do not close this window....
echo .......
SQLCMD -E -S %servername% -Q "declare @fileName nvarchar( 200 ) = N'%backupdir%%databasename%_DIFF_MANUAL_' + REPLACE(convert(nvarchar(20),GetDate(),120),':','-') + N'.bak'; BACKUP DATABASE %databasename% TO DISK=@fileName WITH DIFFERENTIAL, INIT, COMPRESSION, STATS = 10;"

Open in new window


Is that closer to what you were looking for?

Edit:

Looking at the format you wanted, where is that 010000 coming from?
0
 

Author Comment

by:thinkbot
ID: 39255356
@didnthaveaname, yes, thank you.
0

Featured Post

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now