Dynamic table (?)

  • Hi everyone...

    I want to create a SP that retrieve data from tables that have similiar names but different id's at  the end of them for identification purpose.

    For example, all the tables should have the first part of the names the same like Employee but different ids : Employee_100, Employee_101, Employee_102...etc...

    How do i create a select statement for my SP that can retrieve data from different tables each time based on the id's?

    I did create something like this:

    Set @sql = ' select * from Employee'+@tbl_id

    exec (@sql)

    Is there any other way to do just like this by setting a variable for just the table?

    Thanks!

     

     

  • The magic phrase here is "Dynamic SQL" and is something you should avoid, if possible. Read http://www.sommarskog.se/dynamic_sql.html and decide if that is your way to go.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Just out of curiosity, why would you have a table system that has tblEmploye_101, tblEmploye_102.... instead of only 1 table?

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

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