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


Lookup Error - SQL Server Database Error: Must declare the table variable "@table_name".


Lookup Error - SQL Server Database Error: Must declare the table variable "@table_name".

Author
Message
redy007
redy007
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18
Hello guys,

I am new in t-sql (started today). Could you help me with the issue I have?

on line 14 (if EXISTS(SELECT * from @table_name)) the output is
Lookup Error - SQL Server Database Error: Must declare the table variable "@table_name".


SET NOCOUNT ON
DECLARE @count INT, @table_name CHAR(50)
DECLARE @SQL NVARCHAR(MAX)
DECLARE lfray_cursor CURSOR
FOR SELECT NAME FROM sys.objects where [type] = 'U ' and [schema_id] = 6
SELECT @SQL = ''
OPEN lfray_cursor
FETCH NEXT FROM lfray_cursor into @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
--SELECT @SQL = ' Truncate table ' + @table_name
--SELECT @count = SELECT COUNT(*) FROM @table_name
set @table_name = 'LFRAY.' + @table_name;
if EXISTS(SELECT * from @table_name)
print 'aaa'

--exec sp_executesql @SQL
FETCH NEXT FROM lfray_cursor into @table_name
END
CLOSE lfray_cursor
DEALLOCATE lfray_cursor



What I did wrong?

Thanks for help
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16636 Visits: 17024
redy007 (8/6/2013)
Hello guys,

I am new in t-sql (started today). Could you help me with the issue I have?

on line 14 (if EXISTS(SELECT * from @table_name)) the output is
Lookup Error - SQL Server Database Error: Must declare the table variable "@table_name".


SET NOCOUNT ON
DECLARE @count INT, @table_name CHAR(50)
DECLARE @SQL NVARCHAR(MAX)
DECLARE lfray_cursor CURSOR
FOR SELECT NAME FROM sys.objects where [type] = 'U ' and [schema_id] = 6
SELECT @SQL = ''
OPEN lfray_cursor
FETCH NEXT FROM lfray_cursor into @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
--SELECT @SQL = ' Truncate table ' + @table_name
--SELECT @count = SELECT COUNT(*) FROM @table_name
set @table_name = 'LFRAY.' + @table_name;
if EXISTS(SELECT * from @table_name)
print 'aaa'

--exec sp_executesql @SQL
FETCH NEXT FROM lfray_cursor into @table_name
END
CLOSE lfray_cursor
DEALLOCATE lfray_cursor



What I did wrong?

Thanks for help


You can't select from a variable like that. You would have to use dynamic sql.

Since you said this is the first day you have every worked with sql I would make a recommendation. Find another example somewhere that doesn't use a cursor. Cursors are horribly inefficient and should only be used for a few certain tasks. They should not be part of your arsenal as you are learning. The example you found somewhere has two of the most delicate parts of sql. Cursors and dynamic sql. Both are incredibly dangerous when not used correctly.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
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