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

时间:2016-03-18 01:55:01

标签: sql sql-server

我有一个由表名组成的表,例如:

表A

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

如何从名称与表A中TableName列相同的表中选择数据?

例如:

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

谢谢!

5 个答案:

答案 0 :(得分:0)

如果您的联接有效,那么动态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

答案 1 :(得分:0)

您将不得不使用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)

答案 2 :(得分:0)

动态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

答案 3 :(得分:0)

简而言之,不,不是你所要求的。但是,您可以像在this answer中一样使用动态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

只需将结果复制到SQL编辑器中,从最后一项中删除UNION,然后运行整个过程以获得结果。

答案 4 :(得分:0)

我不得不说......这有点疯狂

注意:我向TableA添加了一个ID列作为主键。这需要对行进行唯一标识,以便使用相同的UID和amp;来区分多行。 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