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

Lookup Error - SQL Server Database Error: Must declare the table variable "@table_name". Expand / Collapse
Author
Message
Posted Tuesday, August 6, 2013 8:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, October 11, 2013 2:20 AM
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
Post #1481368
Posted Tuesday, August 6, 2013 8:38 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:31 AM
Points: 13,430, Visits: 12,294
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)
Post #1481375
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse