Solved

sqlcmd add timestamp to output file

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

860 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