根据来自不同表的条件从多个表中提取 select 行的函数
Function to select rows from multiple tables based on conditions from different tables
任何人都可以帮忙解决这个问题吗?
我正在使用 SQL 服务器 2008。 Objective 根据来自不同 table 的条件和值从多个 table 到 select 行。
- 我有 table1, table2, tableN 列作为 ID,ColumnName,ColumnValue 。这些是 table 我需要根据 table
下面的条件 select 行
- 控制 table 列编号、功能和启用
- 存储库 table 包含列 Function 和 tableName
我需要将数字和 ID 作为参数传递,并从启用值 = 1 的控件 table 获取所有函数值的详细信息,并使用这些函数值从存储库 [=] 中收集 table 名称29=]。并且对于从存储库返回的每个 tableName table 使用 ID 值获取所有行。
根据我的理解,您有两个 table 具有这样的架构:
table Control (Number int, Function nvarchar, Enable bit)
table Repository (Function nvarchar, TableName nvarchar)
Control
和 Repositories
通过 Function
列关联。
您还有许多其他 table,这些 table 的名称保存在存储库 table 中。所有这些 table 都有 ID
列。
您想根据数字获取这些 table 名称,然后通过 ID 列从所有这些 table 中获取 select。
如果这确实是您想要做的,下面的代码应该足以解决您的问题。
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
任何人都可以帮忙解决这个问题吗?
我正在使用 SQL 服务器 2008。 Objective 根据来自不同 table 的条件和值从多个 table 到 select 行。
- 我有 table1, table2, tableN 列作为 ID,ColumnName,ColumnValue 。这些是 table 我需要根据 table 下面的条件 select 行
- 控制 table 列编号、功能和启用
- 存储库 table 包含列 Function 和 tableName
我需要将数字和 ID 作为参数传递,并从启用值 = 1 的控件 table 获取所有函数值的详细信息,并使用这些函数值从存储库 [=] 中收集 table 名称29=]。并且对于从存储库返回的每个 tableName table 使用 ID 值获取所有行。
根据我的理解,您有两个 table 具有这样的架构:
table Control (Number int, Function nvarchar, Enable bit)
table Repository (Function nvarchar, TableName nvarchar)
Control
和 Repositories
通过 Function
列关联。
您还有许多其他 table,这些 table 的名称保存在存储库 table 中。所有这些 table 都有 ID
列。
您想根据数字获取这些 table 名称,然后通过 ID 列从所有这些 table 中获取 select。
如果这确实是您想要做的,下面的代码应该足以解决您的问题。
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