Im using Sql2008 trying to run this BCP command but it never creates the file.
-- Export query
DECLARE @qry2 VARCHAR(1000)
SET @qry2 = 'SELECT * FROM @SkippedProductsTabl开发者_如何学运维e'
-- Folder we will be putting the file in
DECLARE @incomingfolder VARCHAR(1000)
SET @incomingfolder = 'c:\Logs'
DECLARE @bcpCommand VARCHAR(2000)
SET @bcpCommand = 'bcp "'+@qry2+'" queryout "'+@incomingfolder+'\SkippedProducts-'+CAST(@StoreMatchCode AS VARCHAR)+'-'+'.txt" -c -T'
PRINT @bcpCommand
EXEC MASTER..xp_cmdshell @bcpCommand, no_output
The created command looks like:
bcp "SELECT * FROM @SkippedProductsTable" queryout "c:\Logs\SkippedProducts-1330-.txt" -c -T
Can anyone suggest what could be going wrong? I've never used BCP before and not really sure where to start looking.
As a start I know that the folder deffinately exists at that location
I think the problem is the SELECT
.
You are SELECT
ing from a table variable that is not declared in the query, so there's nothing for BCP to do.
Table variables only persist for the context they are called in, so even if you have one in a query, and you have dynamic sql or a subproc within that first query, they won't be able to see the table variable.
See this for more info.
精彩评论