Solved

sqlcmd add timestamp to output file

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

19 Experts available now in Live!

Get 1:1 Help Now