Must declare the table variable

  • I found the script on the web to bcp out multiple tables and am trying to change it so that I can export certain tables.

    a) Add the -b switch when I add it, I get an error

    b) Get the count of rows and insert into a log table

    c) Get the tables name and file name

    I keep getting the following error that I should declare variable "@tblname. Is it the exec statement thats causing this ?

    Msg 1087, Level 15, State 2, Procedure prc_BcpOut, Line 49

    Must declare the table variable "@tblname".

    Msg 121, Level 15, State 1, Procedure prc_BcpOut, Line 51

    The select list for the INSERT statement contains more items than the insert list. The number of SELECT values must match the number of INSERT columns.

  • You cannot do this:

    [font="Courier New"]SELECT @Rows = COUNT(*) FROM @tblname[/font]

    To do this, @tblname would have to be a table variable. In your case, it is a varchar that represents the name of the table you want to count. To do this, you need to use dynamic SQL (EXEC() or sp_execute_SQL).

  • As stated above, the parser must be able to resolve the table at compile time. So variables must be declared table variables or a name given.

    You can

    exec(' select * from ' + @tablename)

    or you need to select * from MyTable, and have the tables built in the script.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply