Solved

Rename a file after an xp_cmdshell bcp command

Posted on 2016-10-28
4
74 Views
Last Modified: 2016-10-28
Currently i have a command that reads:

Exec xp_cmdshell 'bcp "Select Expr1, Col9, Col14 from CompanyV Order By Supplier, Expr1 Desc" query out  "C:\bcptestISP.txt" -S HPELITE\MSSQLSERVER12  -T -c -t,'

I would like the "C:\bcptestISP.txt" to be renamed "C:\bcptestISP_getdate().txt"..obviously using the result not the get date()

Is there way for me to do this?
0
Comment
Question by:Mikeyman_01
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Nakul Vachhrajani
ID: 41864297
Well, you can do some string concatenation. Here's a rough draft (I don't have SSMS, so there may be a few things that you need to adjust).

DECLARE @stringDate NVARCHAR(50);
DECLARE @sqlCommand NVARCHAR(MAX);

SELECT @stringDate = CONVERT(NVARCHAR(50),GETDATE(),112); --ISO 112 formats date as yyyymmdd


SELECT @sqlCommand = N'Exec xp_cmdshell ''bcp "Select Expr1, Col9, Col14 from CompanyV Order By Supplier, Expr1 Desc" query out  "C:\bcptestISP' + N'_' + @stringDate + N'.txt" -S HPELITE\MSSQLSERVER12  -T -c -t,'';

EXEC sp_executesql @sqlCommand;

Open in new window

0
 

Author Comment

by:Mikeyman_01
ID: 41864412
I can't figure out what to adjust..I have it written as above..

I am getting

Msg 105, Level 15, State 1, Line 4
Unclosed quotation mark after the character string '.txt" -S HPELITE\MSSQLSERVER12 -T -c -t,';
EXEC sp_executesql @sqlCommand;
'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '.txt" -S HPELITE\MSSQLSERVER12 -T -c -t,';
EXEC sp_executesql @sqlCommand;
'.

Cant figure out where \ what is missing
0
 
LVL 12

Accepted Solution

by:
Nakul Vachhrajani earned 500 total points
ID: 41864551
Trying to work out a query in Notepad is always bad, especially when quotation marks are involved :)

I had one less quotation after the -t. It's fixed in the sample below:

DECLARE @stringDate NVARCHAR(50);
DECLARE @sqlCommand NVARCHAR(MAX);

SELECT @stringDate = CONVERT(NVARCHAR(50),GETDATE(),112); --ISO 112 formats date as yyyymmdd


SELECT @sqlCommand = N'Exec xp_cmdshell ''bcp "Select Expr1, Col9, Col14 from CompanyV Order By Supplier, Expr1 Desc" query out  "C:\bcptestISP' + N'_' + @stringDate + N'.txt" -S HPELITE\MSSQLSERVER12  -T -c -t,''';

--PRINT @sqlCommand;
EXEC sp_executesql @sqlCommand;

--RESULT:
--Exec xp_cmdshell 'bcp "Select Expr1, Col9, Col14 from CompanyV Order By Supplier, Expr1 Desc" query out  "C:\bcptestISP_20161029.txt" -S HPELITE\MSSQLSERVER12  -T -c -t,'

Open in new window

0
 

Author Closing Comment

by:Mikeyman_01
ID: 41864656
Nice.. thanks again for your help..You know your shit!!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

939 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

4 Experts available now in Live!

Get 1:1 Help Now