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"

Article translations Article translations
Article ID: 952734 - View products that this article applies to.
Expand all | Collapse all

On This Page

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))
    GO
    
    INSERT 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 procedure
    CREATE TABLE otemp (field VARCHAR(20))
    GO
    
    INSERT INTO otemp VALUES ('userid')
    INSERT INTO otemp VALUES ('loginid')
    GO
    
    SET QUOTED_IDENTIFIER OFF 
    GO
    
    SET ANSI_NULLS OFF 
    GO
    
    CREATE PROCEDURE [dbo].[testSP]
    AS
    DECLARE @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)
    GO
    
    SET QUOTED_IDENTIFIER OFF 
    GO
    
    SET 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.

Properties

Article ID: 952734 - Last Review: May 30, 2008 - Revision: 1.1
APPLIES TO
  • Microsoft SQL Server 2005 Standard Edition
  • Microsoft SQL Server 2005 Workgroup Edition
  • Microsoft SQL Server 2005 Developer Edition
  • Microsoft SQL Server 2005 Enterprise Edition
Keywords: 
kbexpertiseadvanced kbtshoot kbprb KB952734

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com