You are currently offline, waiting for your internet to reconnect

Error message when you run the bcp utility to copy a result set of a stored procedure that contains a dynamic query in SQL Server 2005: "BCP host-files must contain at least one column"

Extended support for SQL Server 2005 ends on April 12, 2016

If you are still running SQL Server 2005 after April 12, 2016, you will no longer receive security updates and technical support. We recommend upgrading to SQL Server 2014 and Azure SQL Database to achieve breakthrough performance, maintain security and compliance, and optimize your data platform infrastructure. Learn more about the options for upgrading from SQL Server 2005 to a supported version here.

SYMPTOMS
Consider the following scenario. In Microsoft SQL Server 2005, you have a stored procedure that contains a dynamic query. In the stored procedure, the EXEC statement runs the dynamic query. To copy the result set of the stored procedure, you run the bcp utility together with the queryout option. In this scenario, you receive the following error message:
SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Native Client]
BCP host-files must contain at least one column
WORKAROUND
To work around this problem, do not use a dynamic query in a stored procedure when you run the bcp utility together with the queryout option.
STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.
MORE INFORMATION

Steps to reproduce the problem

  1. Start SQL Server Management Studio, and then connect to an instance of SQL Server 2005.
  2. Run the following statements.
    CREATE TABLE users (userid INT,loginid VARCHAR(50))GOINSERT INTO users VALUES (1,'test1')INSERT INTO users VALUES (2,'test2')INSERT INTO users VALUES (3,'test3')INSERT INTO users VALUES (4,'test4')INSERT INTO users VALUES (5,'test5')INSERT INTO users VALUES (6,'test6')INSERT INTO users VALUES (7,'test7')INSERT INTO users VALUES (8,'test8')INSERT INTO users VALUES (9,'test9')INSERT INTO users VALUES (10,'test10')GO-- run the following four lines before you install the stored procedureCREATE TABLE otemp (field VARCHAR(20))GOINSERT INTO otemp VALUES ('userid')INSERT INTO otemp VALUES ('loginid')GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS OFF GOCREATE PROCEDURE [dbo].[testSP]ASDECLARE @f VARCHAR(4000)SELECT @f = COALESCE(@f + ', ', '') + field FROM otemp-- remove the comment marks from the following line and from the earlier coalesce line to make it work-- SET @f = 'userid,loginid'DECLARE @str VARCHAR(4000)SET @str = 'SELECT TOP 10 ' + @f + ' FROM users'EXEC (@str)GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO
  3. At a command prompt, run the following command:
    bcp "execute master..testSP" queryout "C:\test.csv" -c -t\t -SInstanceName -T
    Note In this command, InstanceName is a placeholder for the name of the instance of SQL Server 2005.
bcp stored procedure queryout
Properties

Article ID: 952734 - Last Review: 05/30/2008 19:45:43 - Revision: 1.1

Microsoft SQL Server 2005 Standard Edition, Microsoft SQL Server 2005 Workgroup Edition, Microsoft SQL Server 2005 Developer Edition, Microsoft SQL Server 2005 Enterprise Edition

  • kbexpertiseadvanced kbtshoot kbprb KB952734
Feedback