0

So im going through a schema here Ive never worked with before. There are no relationships between tables.

The initial table has the name of the other tables I need to continue getting info from.

So If I retrieve the table name from the initial table, how can I use that table name in the same stored procedure later on?

The initial table is AE_Table1. It has a number of columns. 2 of those contain the names of the tables I need for each entry. So

Select tb2Name
from AE_Table1
where ID = 1

Will give me the name of the first table I need. Then I need to store that value in a variable, table2Name for example, and use it later.

Select  *
from @table2Name
Where xyz

So is there a way for me do do this? (please forgive any mistakes above, Im just writing this quickly to show what I need, its not meant to be 100% syntactically correct)

Thanks.

discodowney
  • 1,475
  • 6
  • 28
  • 58
  • 1
    You will need to use dynamic SQL (look up `sp_executesql` for the best way to do it). – Gordon Linoff Jul 30 '15 at 14:50
  • Your database schema is very badly designed. The only way I see is to build a dynamic SQL string for each `ID`. – Code Different Jul 30 '15 at 14:51
  • 1
    You will have to use dynamic SQL. You need to read and understand this because you will be doing a lot of this with this very bad database design. http://www.sommarskog.se/dynamic_sql.html – HLGEM Jul 30 '15 at 14:51
  • Your all preaching to the choir. I think they did it this way so if you ever need to migrate the data out its as big a pain in the arse as possible, so most people will just pay them to do it. Cheers for the info. – discodowney Jul 30 '15 at 14:53

0 Answers0