1

I have table named Master which contains a column outtablename that has data which are also table names having columns score and more. I want to fetch data from these tables dynamically.

Master table:

outtablename
==============
fte
wdp

and fte have data like below

fte table:

sku   score
=============
1        23
2        34

And I want to access data in fte dynamically

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
indra_patil
  • 283
  • 1
  • 4
  • 11
  • 1
    Welcome to Stack Overflow! I have no idea what you are trying to ask. Please post the full definition (CREATE TABLE script) of each of your tables, as well as some sample data from each. Then, describe the relationship between those tables and post a sample of what you've tried to do and what you want the end product to look like. – AHiggins Jul 08 '14 at 14:04
  • Can you please give some example query? – Amit Prajapati Jul 08 '14 at 14:13
  • Check out the answer to http://stackoverflow.com/questions/14026823/variable-table-name-using-dynamic-sql-in-c-sharp – Jenn Jul 08 '14 at 17:03

1 Answers1

0

You can do it using this query.

declare @outtablename varchar(256)
declare @STR Nvarchar(max)

declare tbl cursor
for select [outtablename] from Master

Open tbl
fetch next from tbl into @outtablename

while(@@fetch_status = 0)
begin 
   SET @STR ='select * from '+outtablename
   EXEC sp_executesql @STR 
   fetch next from tbl into @outtablename
end
close tbl
deallocate tbl
Uttam Kasundara
  • 237
  • 3
  • 13