I have a table consists of table name, for example:
TableA
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?
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..
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
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:
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:
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.
i have to say... this is a bit crazy
Note : I added a ID column to TableA as a primary key. This is required to uniquely identity the rows to differenciate multiple rows with same 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.