Results 1 to 3 of 3

Thread: Query a dynamic table...HELP!

  1. #1
    Join Date
    May 2003
    Posts
    1

    Question Query a dynamic table...HELP!

    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!!!!

  2. #2
    Join Date
    Mar 2003
    Location
    NJ
    Posts
    201
    (1)
    According to your original post.It seems you only create one table per day.
    Because if you have more than one table.You will get a error (subquery cant have multiple returns)

    ----------------------------------------
    (2)here is the modified query
    Instead of 50 ,put 128 instead!

    declare @tablename varchar(128) set @tablename = (
    select name from sysobjects where left(crdate,10) = left(convert(datetime,'11/13/1998'),10)
    )

    ---------------------------------------
    (3)You can run this to see the result first.If you have many table name returns,you have to put the result into a table and use loop or cursor to retrive row by row

    select name from sysobjects where left(crdate,10) =
    left(convert(datetime,'11/13/1998'),10)

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    if it is only one table
    ------------------------
    declare @tablename varchar(128)
    set @tablename = (select top 1 name from sysobjects where convert(varchar(8),crdate,112) = '20030501' and type ='u')
    exec ('select * from '+ @tablename )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •