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

  • 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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

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

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