Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Query a dynamic table...HELP!

jshulljshull Posts: 2Member
Hello,

I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:

declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename

But I get an error on 'select * from @tablename' (must declare @tablename)

Any assistance is GREATLY APPRECIATED!!!!

Comments

  • DaedaliusDaedalius Posts: 30Member
    I think what you want to do is create a string and then you want to execute the string:

    DECLARE @SQL NVARCHAR(128);
    SET @SQL = N'SELECT * FROM '+@tablename+';'
    EXECUTE sp_executesql @SQL


    : Hello,
    :
    : I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
    :
    : declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
    :
    : But I get an error on 'select * from @tablename' (must declare @tablename)
    :
    : Any assistance is GREATLY APPRECIATED!!!!
    :

  • jshulljshull Posts: 2Member
    Worked like a charm. Thank you for the help!!!!


    : I think what you want to do is create a string and then you want to execute the string:
    :
    : DECLARE @SQL NVARCHAR(128);
    : SET @SQL = N'SELECT * FROM '+@tablename+';'
    : EXECUTE sp_executesql @SQL
    :
    :
    : : Hello,
    : :
    : : I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
    : :
    : : declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
    : :
    : : But I get an error on 'select * from @tablename' (must declare @tablename)
    : :
    : : Any assistance is GREATLY APPRECIATED!!!!
    : :
    :
    :

  • rpalserrpalser Posts: 1Member
    I am trying to declare a table as a variable use the code below, however in query analyzer I still get the error "Must Declare the Variable @tablename". How did you get it to "work like a charm?"

    : Worked like a charm. Thank you for the help!!!!
    :
    :
    : : I think what you want to do is create a string and then you want to execute the string:
    : :
    : : DECLARE @SQL NVARCHAR(128);
    : : SET @SQL = N'SELECT * FROM '+@tablename+';'
    : : EXECUTE sp_executesql @SQL
    : :
    : :
    : : : Hello,
    : : :
    : : : I am trying to write a query to pull data from a table that is system generated daily at midnight. I tried:
    : : :
    : : : declare @tablename varchar(50) set @tablename = (select name from sysobjects where crdate = '05/01/2003') select * from @tablename
    : : :
    : : : But I get an error on 'select * from @tablename' (must declare @tablename)
    : : :
    : : : Any assistance is GREATLY APPRECIATED!!!!
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.