简体   繁体   English

如何从多个表中选择数据,其中表名是另一个表的列中的值?

[英]How to select data from multiple tables where table name is value from a column of another table?

I have a table consists of table name, for example: 我有一个由表名组成的表,例如:

TableA 表A

    UID  TableName  CifKey  ...
    1     xxx       12345
    1     yyy       12345
    1     xxx       12345
    2     zzz       45678

How can I select data from tables that having name same as the TableName column in Table A? 如何从名称与表A中的TableName列相同的表中选择数据?

For example: 例如:

SELECT A.a, B.b 
FROM TableA A 
JOIN ' + @TableName + ' B ON A.Cifkey = B.Cifkey 
WHERE A.uid = @uid AND A.cifkey = @cifkey

Thank you! 谢谢!

If your join works,dynamic sql is the way you want to accomplish the task.. 如果您的联接有效,则动态sql是您想要完成任务的方式。

declare @tablename nvarchar(100)
select @tablename=tablename from yourtable where uid=@uid and cifkey=@cifkey

declare @sqlquery nvarchar(4000)
set @sqlquery='SELECT A.a, B.b FROM TableA A 
JOIN ' + @TableName + ' B 
ON A.Cifkey = B.Cifkey 
WHERE A.uid = @uid and A.cifkey = @cifkey';

exec sp_executesql @sqlquery,
N'@uid int,@cifkey int',
@uid=10,@cifkey=100

You will have to use a dyanmic query - something like this 你将不得不使用dyanmic查询 - 像这样

DECLARE @tablename VARCHAR(50)
DECLARE @uid INT = 1
DECLARE @cifkey INT = 12345
SELECT @tablename = TableName FROM TableA WHERE uid = @uid AND cifkey = @cifkey

DECLARE @query VARCHAR(500)

SELECT @query = 'select A.a, B.b from TableA A JOIN ' + @tablename + ' B 
ON A.Cifkey = B.Cifkey 
WHERE A.uid = ' + CONVERT(VARCHAR(20),@uid) + ' and A.cifkey = ' + CONVERT(VARCHAR(20), @cifkey)

EXEC(@query)

Dynamic SQL is the way to go: 动态SQL是必经之路:

DECLARE @uid    INT = 1,
        @cifkey INT = 12345

DECLARE @sql NVARCHAR(MAX) = ''

SELECT @sql = @sql +
'SELECT
    A.a, B.b -- Replace with correct column names
FROM TableA A
JOIN ' + QUOTENAME(TableName) + ' B
    ON A.Cifkey = B.Cifkey
WHERE
    A.uid = @uid 
    AND A.cifkey = @cifkey
UNION ALL
'
FROM (
    SELECT DISTINCT TableName FROM TableA WHERE UID = @uid AND cifkey = @cifkey
) t

IF @sql <> '' BEGIN
    -- Remove the last UNION ALL
    SELECT @sql = LEFT(@sql, LEN(@sql) - 11)

    PRINT @sql

    EXEC sp_executesql
        @sql,
        N'@uid INT, @cifkey INT',
        @uid,
        @cifkey
END

In short, no, not as you are asking. 简而言之,不,不是你所要求的。 You can, however, use dynamic SQL as in this answer to create a series of SELECT statements that could be merged with UNION s to produce your desired results, like this: 但是,您可以在此答案中使用动态SQL来创建一系列SELECT语句,这些语句可以与UNION合并以生成所需的结果,如下所示:

SELECT DISTINCT 'SELECT A.a, B.b FROM TableA A 
        JOIN ' + TableName + ' B 
        ON A.Cifkey = B.Cifkey 
        WHERE A.uid = @uid and A.cifkey = @cifkey
        UNION
        '
FROM TableA

Just copy the results into the SQL editor, remove the UNION from the last item, and run the whole thing to get your results. 只需将结果复制到SQL编辑器中,从最后一项中删除UNION ,然后运行整个过程以获得结果。

i have to say... this is a bit crazy 我不得不说......这有点疯狂

Note : I added a ID column to TableA as a primary key. 注意:我向TableA添加了一个ID列作为主键。 This is required to uniquely identity the rows to differenciate multiple rows with same UID & Cifkey 这是唯一标识行以使用相同的UID和Cifkey区分多个行所必需的

-- Create the table
create table TableA
(
    ID      int identity,
    UID     int,
    TableName   varchar(5),
    CifKey      int,
    a       int
)

create table xxx
(
    CifKey      int,
    b       int
)

create table yyy
(
    CifKey      int,
    b       int
)

create table zzz
(
    CifKey      int,
    b       int
)

set nocount on

-- Insert some sample data
insert into TableA select 1, 'xxx', 12345, 1
insert into TableA select 1, 'yyy', 12345, 2
insert into TableA select 1, 'xxx', 12345, 3
insert into TableA select 2, 'zzz', 45678, 4

insert into xxx select 12345, 100
insert into yyy select 12345, 200
insert into zzz select 45678, 300

-- the Dynamic SQL query
declare @sql    nvarchar(max)

select  @sql    = N'SELECT A.a, b = COALESCE('

select  @sql    = @sql +
        + N'B' + convert(varchar(10), ID) + '.b,'
from    TableA
order by ID

select  @sql    = left(@sql, LEN(@sql) - 1) + ')' + char(13)

select  @sql    = @sql 
        + N'FROM TableA A'  + char(13)

; with cte as
(
    select  *, rn = convert(varchar(10), ID)
    from    TableA
)
select  @sql    = @sql 
        + N'LEFT JOIN ' + quotename(TableName) + ' AS B' + rn + ' ' + char(13)
        + N'ON  A.CifKey    = B' + rn + '.CifKey' + char(13)
        + N'AND A.ID        = ' + rn + char(13)
from    cte

print   @sql 
exec    sp_executesql @sql

set nocount off

-- clean up 
drop table TableA
drop table xxx
drop table yyy
drop table zzz

-- The generated Query (print @sql)
SELECT A.a, b = COALESCE(B1.b,B2.b,B3.b,B4.b)
FROM TableA A
LEFT JOIN [xxx] AS B1 
ON  A.CifKey    = B1.CifKey
AND A.ID        = 1
LEFT JOIN [yyy] AS B2 
ON  A.CifKey    = B2.CifKey
AND A.ID        = 2
LEFT JOIN [xxx] AS B3 
ON  A.CifKey    = B3.CifKey
AND A.ID        = 3
LEFT JOIN [zzz] AS B4 
ON  A.CifKey    = B4.CifKey
AND A.ID        = 4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL-从表中选择列,其中另一个表的列值等于列名列表 - SQL - Select Columns from table where another table column value equals column name list 在SQL中联接表,其中新列名基于另一列的联接表值 - Joining tables in SQL where new column name is based on joined table value from another column select 列具有特定值的所有表中的表名 - select table name from all tables where column has specific value SQL 从包含表名、列名和值列映射的数据表中插入数据到多个表中可以在源中更改 - SQL Insert data into multiple tables from data table that contains table name, column name and value column mapping can change in source MySQL多插入从另一个表中选择多列 - mysql multiple Insert where select many column from another table 如何从表中选择取决于另一个表列中的值 - How to select from table depend on value from another table column 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? 如何通过从 SQL Server 中的另一个表中选择列名和值将行插入表中 - How to insert row into a table by select column name and value from another table in SQL Server 从表中选择值的sql语句,该表的名称来自另一个表 - sql statement to select value from a table where that table's name is from another table 选择表中的列与另一个表中的列不同的数据 - Select data where column from table is different from column from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM