|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5,
Visits: 20
|
|
Hi,
How can I insert current date in BCP out file name?
I use this command, in SQL JOB step (type: Operating System ComdExec) bcp "SELECT * FROM NW.dbo._au_NAPI" queryout "E:\kon_DATE.csv" -CACP -c -T -t"\";\"" -r"\"\"" -S DB
Have you any ideas?
Thanks, gmac
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:56 AM
Points: 1,315,
Visits: 2,885
|
|
you are running bcp from a shell, so get the current date (M/D/Y) part into a variable:
set DATEVAR=%DATE:~4,10%
as part of a filename you probably want to change the / to - like this:
set mydate=%DATEVAR:/=-%
then you can use %mydate% in your batch command wherever you want the current date, like today would be: 02-20-2013
The probability of survival is inversely proportional to the angle of arrival.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:32 PM
Points: 32,906,
Visits: 26,792
|
|
sturner (2/20/2013) you are running bcp from a shell, so get the current date (M/D/Y) part into a variable:
set DATEVAR=%DATE:~4,10%
as part of a filename you probably want to change the / to - like this:
set mydate=%DATEVAR:/=-%
then you can use %mydate% in your batch command wherever you want the current date, like today would be: 02-20-2013
What language are you using? This doesn't appear to be T-SQL.
Never mind. I get it. These are batch commands at the command line level. Well done.
Can you think of a way to have the date come out in the ISO format of YYYYMMDD to support sortable file names using the batch commands?
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:56 AM
Points: 1,315,
Visits: 2,885
|
|
Jeff Moden (2/20/2013)
Can you think of a way to have the date come out in the ISO format of YYYYMMDD to support sortable file names using the batch commands?
didling with the String manipulation function in shell is a pain, but yes you can do it.
from what I understand you have to play some tricks to combine variables. (I looked at this: http://stackoverflow.com/questions/7846560/how-concatenate-two-variables-in-batch-script)
anyway, I would start by getting each part of the current DATE into three variables:
set MM=%DATE:~4,2% set DD=%DATE:~7,2% set YY=%DATE:~10,4%
then create your new yymmdd variable by combining them: setlocal EnableDelayedExpansion
set yyyymmdd=!%YY%%MM%%DD%!
I hate messing with batch scripting because it is so ugly (not like unix shells) but if you are a DBA you end up needing to use it at times.
The probability of survival is inversely proportional to the angle of arrival.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5,
Visits: 20
|
|
If I insert variable in SQL JOB Step, the JOB exec stopped with error. When I insert throughout code (variables, bcp command...) in .bat file, and I reference this file in SQL JOB, the JOB was successfully executed.
This is very interesting! Why get error, if code in JOB step, and why not if code insert in .bat file?
Sorry for my english!
Thanks gmac
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:56 AM
Points: 1,315,
Visits: 2,885
|
|
gmac 41947 (2/21/2013) If I insert variable in SQL JOB Step, the JOB exec stopped with error. When I insert throughout code (variables, bcp command...) in .bat file, and I reference this file in SQL JOB, the JOB was successfully executed.
This is very interesting! Why get error, if code in JOB step, and why not if code insert in .bat file?
Those variables and string manipulation functions are native to the DOS command shell only. So you have to be running from a .bat file to use them.
Now that you have told me this was a SQLAgent job step, you could have generated file name with the current date in it using SQL, then passed the file name as a parameter to the .bat file.
Anyway, glad you got it working.
The probability of survival is inversely proportional to the angle of arrival.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Today @ 8:23 AM
Points: 323,
Visits: 2,007
|
|
Since you are running a SQL Server Agent job, you can use SQL Server Agent tokens. The one for date the Job started running is: $(ESCAPE_NONE(STRTDT))
Here's a link to the 2008R2 help...
http://msdn.microsoft.com/en-us/library/ms175575.aspx
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:48 AM
Points: 5,
Visits: 20
|
|
PhilPacha (2/21/2013) Since you are running a SQL Server Agent job, you can use SQL Server Agent tokens. The one for date the Job started running is: $(ESCAPE_NONE(STRTDT))
Here's a link to the 2008R2 help...
http://msdn.microsoft.com/en-us/library/ms175575.aspx
Great, thank you! I'm going to try it!
gmac
|
|
|
|