-4

Can anyone please help with this query?

I’m using SQL server 2008 . Objective is to select rows from multiple tables based on condition and values from different tables .

  1. I have table1, table2, tableN with columns as ID,ColumnName,ColumnValue . These are the table I need to select rows based on conditions from below table
  2. Control table with columns Number,Function and Enable
  3. Repository table with columns Function and tableName

I need pass Number and ID as parameters and get details of all Function values from Control table which has Enable value = 1 and by using these Function values collect tableNames from Repository table . And for each tableName returned from Repository table get all rows by using ID value.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
AHS
  • 87
  • 1
  • 9
  • It is VERY difficult to understand your schema and requirements without seeing your schema. Can you share some sample data from these tables and your desired results? – JNevill Jan 09 '18 at 20:53
  • 1
    "Can anyone please help with this query?" I don't see any query!!! Learn how to ask question. Then maybe someone will help. https://stackoverflow.com/help/how-to-ask – Eric Jan 09 '18 at 22:14
  • 1
    Please read [this](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) for some tips on improving your question. – HABO Jan 09 '18 at 22:36
  • Thank you for help doc , I’ll improve my how to ask skill ! – AHS Jan 09 '18 at 22:44

1 Answers1

1

The way I understand it you have two tables with schema like this:

table Control (Number int, Function nvarchar, Enable bit)
table Repository (Function nvarchar, TableName nvarchar)

Control and Repositories are related via Function column.

You also have a number of other tables and names of those tables are saved in Repositories tables. All those tables have ID column.

You want to get those table names based on a number and then select from all those tables by their ID column.

If that indeed is what you are trying to do, code bellow should be enough to solve your problem.

declare
    -- arguments
    @id int = 123,
    @number int = 123456,
    -- helper variables we'll use along the way
    @function nvarchar(4000),
    @tableName nvarchar(256),
    @query nvarchar(4000)

-- create cursor to iterate over every returned row one by one
declare cursor #tables readonly fast_forward
for
select
    c.Function,
    r.TableName
from [Control] as c
join [Repository] as r on r.Function = c.Function
where c.Number = @number
and c.Enable = 1

-- initialise cursor
open #tables
-- get first row into variables
fetch next from #tables
    into @function, @tableName

-- will be 0 as long as fetch next returns new values
while @@fetch_status = 0
begin
    -- build a dynamic query
    set @query = 'select * from ' + @tableName + ' where ID = ' + @id

    -- execute dynamic query. you might get permission problems
    -- dynamic queries are best to avoid, but I don't think there's another solution for this
    exec(@query)

    -- get next row
    fetch next from #tables
        into @function, @tableName
end

-- destroy cursor
close #tables
deallocate #tables
Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37