using BCP to export stored procedure result in SQL Server 2008

六月ゝ 毕业季﹏ 提交于 2019-12-08 05:45:30

问题


Heyy,

I'm trying to use BCP to export a SP result to a text file using this query:

EXEC xp_cmdshell 'bcp "exec asmary..usp_Contract_SelectByEmpId -1,1" queryout "C:\test.txt" -w -C OEM -t$ -T -r ~ -S heba\HEBADREAMNET '

The output of this query is telling this error:

Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'where'.

even thought I'm sure that the stored procedure "usp_Contract_SelectByEmpId" is working correctly.

Anyone faced that kind of error before?


回答1:


  1. As Lynn suggested, check your stored procedure. It looks like the issue is within that.

  2. Ensure any plain SELECT works (e.g., C: drive is database server's local drive, not necessarily your own local drive).

  3. If the first two items work fine, then add SET FMTONLY OFF as follows:

EXEC xp_cmdshell 'bcp "set fmtonly off exec asmary..usp_Contract_SelectByEmpId -1,1" queryout "C:\test.txt" -w -C OEM -t$ -T -r ~ -S heba\HEBADREAMNET'

I have to admit that when I tried similar on my computer it failed with 'Function sequence error', and I found that it is related to a SQL Server 2008 bug fixed in 2011.

Please note also that even without SET FMTONLY OFF everything works with BCP library (odbcbcp.dll/odbcbcp.lib). So, you can have much more generic ODBC-wide bcp solution if you write your own wrapper executable (for instance, in C or C++).

I also found the following at http://msdn.microsoft.com/en-us/library/ms162802.aspx

The query can reference a stored procedure as long as all tables referenced inside the stored procedure exist prior to executing the bcp statement. For example, if the stored procedure generates a temp table, the bcp statement fails because the temp table is available only at run time and not at statement execution time. In this case, consider inserting the results of the stored procedure into a table and then use bcp to copy the data from the table into a data file.

Please see also my later separate reply - I think the whole concept of using stored procedure for BCP/queryout is wrong.




回答2:


Try this.

DECLARE @strbcpcmd NVARCHAR(max)
SET @strbcpcmd = 'bcp  "EXEC asmary..usp_Contract_SelectByEmpId -1,1" queryout "C:\test.txt" -w -C OEM -t"$" -T -S'+@@servername    
EXEC master..xp_cmdshell @strbcpcmd



回答3:


Sorry for flooding your question with multiple answers, but I wanted to find out how much heavier (performance-wise) the use of stored procedure is compared to plain SELECT. And I got a very important information from

http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/b8340289-7d7e-4a8d-b570-bec7a0d73ead/

This forced me to create another (separate) answer. The post I refer to invalidates the whole concept.

In a few words: stored procedure might be called several (3) times in order to figure out the structure of the resultset, then the actual data.

Therefore (and especially if calling from SQL Server connection rather than client), I think it makes a lot more sense to have a stored procedure or function, which will return SELECT statement. Then you can have another generic stored procedure or function to create and execute full BCP command with that statement embedded. I am pretty sure that in this case BCP might use a lot better execution plan. Unfortunately, I cannot verify that in practice, because of BCP bug in SQL Server 2008 R2 I mentioned in my previous post.

N.B. Please be careful creating dynamic queries and escape all explicit literal strings (i.e. repeat all single quotes twice) in order to avoid notorious SQL injection. Unfortunately, there is another pitfall: you should ensure you are not escaping your queries twice or more times.



来源:https://stackoverflow.com/questions/11095355/using-bcp-to-export-stored-procedure-result-in-sql-server-2008

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!