Author |
Topic |
raaj
Posting Yak Master
129 Posts |
Posted - 2013-07-12 : 01:03:16
|
HiI am scheduling a SQL job to run everyday and it has the following SQL statement :EXEC master..xp_cmdshell 'BCP "Select * from AdventureWorks.dbo.Employees" queryout "\\SERV120\C$\test.csv" -T -c -t, -r \n -R' As per the above SQL statement whenever the SQL job runs, the data from the query will be exported to server SERV120 (in C drive) with name test.csvNow what I want is I would like to have that file created everyday with date (like test_dateexported) so that I can see all the csv files created daily. So how to modify the above SQL statement so that I can include the runtime in the filename extension?Thanks,Ravi |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 01:44:07
|
you need to use dynamic sql for that something likeDECLARE @SQL varchar(2000),@Date varchar(11)SET @Date=CONVERT(varchar(11),GETDATE(),101)SET @SQL='EXEC master..xp_cmdshell ''BCP "Select * from AdventureWorks.dbo.Employees" queryout "\\SERV120\C$\test" + @Date + ".csv" -T -c -t, -r \n -R'''EXEC(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-07-12 : 01:49:14
|
Thanks for u r reply visakh.But I am getting the follwing error :Unknown argument '+' on command line. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 01:57:47
|
ah ...a typoit should be ' and not "DECLARE @SQL varchar(2000),@Date varchar(11)SET @Date=CONVERT(varchar(11),GETDATE(),101)SET @SQL='EXEC master..xp_cmdshell ''BCP "Select * from AdventureWorks.dbo.Employees" queryout "\\SERV120\C$\test' + @Date + '.csv" -T -c -t, -r \n -R'''EXEC(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-07-12 : 02:04:38
|
visakh, still getting the following error :outputSQLState = S1000, NativeError = 0Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-fileNULL |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 02:14:46
|
is the path \\SERV120\C$\ accessible from the machine where you're trying to execute this?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-07-12 : 02:18:37
|
yes definitely it is accessible.Initially, when i ran the SQL query which I posted in the original post, it worked and when i went to that server, i am able to see the test.csv file.But, after making the change to include @Date, it was giving that error. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 02:22:00
|
ok can you make this small change and post the result obtained?DECLARE @SQL varchar(2000),@Date varchar(11)SET @Date=CONVERT(varchar(11),GETDATE(),101)SET @SQL='EXEC master..xp_cmdshell ''BCP "Select * from AdventureWorks.dbo.Employees" queryout "\\SERV120\C$\test' + @Date + '.csv" -T -c -t, -r \n -R'''PRINT(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 02:27:05
|
ok i got the issue.try this insteadDECLARE @SQL varchar(2000),@Date varchar(11)SET @Date=CONVERT(varchar(11),GETDATE(),112)SET @SQL='EXEC master..xp_cmdshell ''BCP "Select * from AdventureWorks.dbo.Employees" queryout "\\SERV120\C$\test' + @Date + '.csv" -T -c -t, -r \n -R'''EXEC(@SQL) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-07-12 : 02:37:06
|
Thanks a lot visakh. That worked perfectly. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-12 : 02:46:18
|
you're welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-09-05 : 02:32:09
|
Hi Visakh,I just realised there is one more issue I am facing :In one of my columns (column Name is ServiceName) there is a value called Plan, Environment, Liabilityi.e the value is comma seperated.Now, when the query is run in SQL, it is showing perfectly correct result set.But in csv, the commas are disappearing and the value is splitting into three different cells.Is there anyway that I can make the value appear as it is showing in the SQL resultset.Thanks,raaj |
|
|
raaj
Posting Yak Master
129 Posts |
Posted - 2013-09-05 : 02:32:45
|
Hi Visakh,I just realised there is one more issue I am facing :In one of my columns (column Name is ServiceName) there is a value called Plan, Environment, Liabilityi.e the value is comma seperated.Now, when the query is run in SQL, it is showing perfectly correct result set.But in csv, the commas are disappearing and the value is splitting into three different cells.Is there anyway that I can make the value appear as it is showing in the SQL resultset.Thanks,raaj |
|
|
|