Solved

sqlcmd add timestamp to output file

Posted on 2013-06-12
4
2,682 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
[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
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

630 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