Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Must declare the table variable Expand / Collapse
Author
Message
Posted Monday, August 18, 2008 6:28 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Sunday, April 26, 2009 7:37 AM
Points: 87, Visits: 307
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.





  Post Attachments 
prc_BcpOut.txt (25 views, 4.81 KB)
Post #554249
Posted Monday, August 18, 2008 7:04 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,661, Visits: 23,078
You cannot do this:

SELECT @Rows = COUNT(*) FROM @tblname

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).

Post #554274
Posted Monday, August 18, 2008 7:10 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 4:56 PM
Points: 31,168, Visits: 15,612
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #554281
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse