Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

BCP Utility to output contents into CSV Expand / Collapse
Author
Message
Posted Tuesday, May 21, 2013 5:28 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 7:21 PM
Points: 135, Visits: 101
I have a stored procedure that gets a SQL query, file path (to output) and file name as parameter which I'm using to build a bcp command and finally executing the same e.g

SET @sql = 'bcp "'
+ @query
+ '" queryout "
+ @path_nm
+ @file_nm
+ '" -c'
+ ' -t,'
+ ' -T'

EXECUTE MASTER..xp_cmdshell @sql, NO_OUTPUT
Upon executing the stored proc it doesn't produce the file specified in the parameter.

I have verified the parameters and they are correct. I did a print to see what's in @sql and say I get the following -

'bcp "SELECT [col1],[col2],[col3] FROM [##results]" queryout "E:\test\test.csv" -c -t, -T'
On executing the command standalone (i.e. not from a stored proc) from SSMS produces the output file correctly e.g.

EXEC MASTER..xp_cmdshell 'bcp "SELECT [col1],[col2],[col3] FROM [##results]" queryout "E:\test\test.csv" -c -t, -T', NO_OUTPUT
Confused!

Are there any limitations on running BCP utility from a stored proc - am I missing something very obvious?

Please help!

Thanks, Bikram
Post #1454933
Posted Tuesday, May 21, 2013 11:14 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 11:24 AM
Points: 33,088, Visits: 15,197
I would check permissions. Doesthe SQL Server service account have permissions to the e:\test drive? It likely doesn't by default.






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1455135
Posted Tuesday, May 21, 2013 2:35 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 7:21 PM
Points: 135, Visits: 101
Hi Steve,

I thought about that and I had checked the permission - seems fine (SQL Server users have special permission to create files / append data etc). Also I would have thought if permission was an issue then the result would be same when running from SSMS just by itself. What is confusing me is the difference in result between running from a Stored Procedure and running the same by itself as mentioned in my post earlier.

As a sysadmin on the server - should permission be an issue for sysadmin?

Thanks,
Bikram
Post #1455221
Posted Tuesday, May 21, 2013 2:47 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 12,887, Visits: 31,835
bikram.g.it (5/21/2013)
I thought about that and I had checked the permission. I would have thought if permission was an issue then the result would be same when running from SSMS just by itself. What is confusing me is the difference in result between running from a Stored Procedure and running the same by itself as mentioned in my post earlier.


my guess is that the calling user ends up using the proxy account that might be set up, whereas a sysadmin user, when going to xp_cmdshell, always uses the context of the startup account.

do something like this to test it yourself:
EXECUTE AS USER='Bob'
EXECUTE MyProcedure @MyParameters
--note the error message
REVERT; --change back to sysadmin

The proxy account for normal users, if it is set up:



The startup account, which is used if the proxy is blank, or the user is a sysadmin



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1455226
Posted Tuesday, May 21, 2013 3:06 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 11:24 AM
Points: 33,088, Visits: 15,197
Lowell has the potential issue. A proxy could do this for sure.

Other than that, I'd still guess it's some weird permissions thing. You could to a

xp_cmdshell "dir > myfile.txt"

and then see where myfile.txt ends up, if it does. Working through different commands might help you track things down if it's not the proxy.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1455229
Posted Tuesday, May 21, 2013 3:32 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 7:21 PM
Points: 135, Visits: 101
Lowell / Steve,

Some good thoughts - the proxy account I had setup is using the admin user. So that should be fine I guess.

I tried the dir test e.g. EXEC MASTER..xp_cmdshell 'dir E:\test\*.*> E:\test\myfile_1.txt', NO_OUTPUT

this runs perfectly fine both from a SSMS session and from a stored proc.

I guess it is probably something to with BCP Utility fired off a stored proc. But not entirely sure!
Post #1455237
Posted Tuesday, May 21, 2013 3:50 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 2:11 PM
Points: 897, Visits: 7,098
Could you add the fully qualified path name for the bcp executable, and an explict server with -S and try it again? And just as a long shot, you're not also connecting to Sybase from this machine are you? Also, adding the -e option to write to an error file might provide additional diagnostic info.



And then again, I might be wrong ...
David Webb
Post #1455242
Posted Tuesday, May 21, 2013 4:22 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 7:21 PM
Points: 135, Visits: 101
Hi David,

We are no where near Sybase. -S / server option I have already tried but I guess it doesn't make much difference as the whole deal is on the same server. Re fully qualified path for BCP executable - tried this now and same result. Tried the -e option as well no error out created so I guess no errors in processing the command - not entirely sure if -e works for queryout; think it is only for IN operation (may be wrong).

Thanks,
Bikram
Post #1455249
Posted Tuesday, May 21, 2013 4:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 11:24 AM
Points: 33,088, Visits: 15,197
Can you set a login trigger and look for the login from BCP to the instance? I wonder if there's some issue connecting back from BCP.

Can you dir the path you have in the command to a file with xp_cmdshell? Ensure that's correct. Also, can you perhaps hard code the BCP command that you run in SSMS in the proc and see what happens?







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1455257
Posted Tuesday, May 21, 2013 6:00 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 28, 2014 7:21 PM
Points: 135, Visits: 101
OK - so I modified the stored proc a bit to have something like this -

EXECUTE @RESULT = MASTER..xp_cmdshell 'bcp "SELECT COL1, COL2 FROM [##results]" queryout "E:\test\TEST1.csv" -c -t, -T -S DWETLSERV1 -oE:\test\BCPCMD_1.txt', NO_OUTPUT
EXEC MASTER..xp_cmdshell 'dir "C:\Program Files\Microsoft SQL Server\90\Tools\binn\*.exe"> E:\test\myfile_1.txt', NO_OUTPUT
EXECUTE @RESULT = MASTER..xp_cmdshell @sql_1, NO_OUTPUT

@sql contains bcp "SELECT COL1, COL2 FROM [##results]" queryout "E:\test\TEST2.csv" -c -t, -T -S DWETLSERV1 -oE:\test\BCPCMD_2.txt


I got TEST1.csv, BCPCMD_1.txt & myfile_1.txt where TEST1.csv contains the result of the SQL query BCPCMD_1.txt contains the output of the first BCP command and myfile_1.txt contains all the exe files in the location mentioned (including bcp) and I have checked the path as well that has the location set as well.

But I didn't get TEST2.csv & BCPCMD_2.txt.

Seems xp_cmdshell is getting trouble running @sql!

Post #1455270
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse