Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Must declare the table variable


Must declare the table variable

Author
Message
raym
raym
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 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.



Attachments
prc_BcpOut.txt (28 views, 4.00 KB)
Michael Earl-395764
Michael Earl-395764
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2701 Visits: 23078
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).
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36000 Visits: 18728
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
My Blog: www.voiceofthedba.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search