从[从另一个表的列]中选择SQL查询。

时间:2022-02-28 09:31:17

I have a table X where a trigger will insert a row when there's a changes to some tables. I've inserted the table name into table X.

我有一个表X,当某些表发生更改时,触发器将插入一行。我已经将表名插入到表X中。

Now, I would like to select the data from table X while inner join with the actual table itself. Is it possible by using a value from a column of the select table as the table for inner join?

现在,我想从表X中选择数据,同时使用实际的表本身进行内部连接。是否可以使用select表的列中的值作为内部连接的表?

The query should looks something like this

查询应该是这样的

SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [X.TableName] AS Y ON Y.ID = X.ID

3 个解决方案

#1


3  

Executing

执行

select 'SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [' + X.TableName + '] AS Y ON X.ID = Y.ID 
where x.primarykey =' + x.primarykey from x

Will output a series of sql statements like

会输出一系列sql语句吗

SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [ customer ] AS Y ON X.ID = Y.ID
where x.primarykey = 1234

that you can then execute "sql to build sql" if you will.

然后您可以执行“sql来构建sql”。

#2


1  

No, that is not possible. You can't use values as table names directly in a query, and you can't join each record against a different table.

不,那是不可能的。不能在查询中直接将值用作表名,也不能将每个记录连接到不同的表。

You would have to make the join for a single record, and create the query dynamically to use a value as table name:

您必须为单个记录创建连接,并动态创建查询,以使用值作为表名:

declare @name varchar(50)
set @name = select TableName from X where ID = 42
exec('select X.a, Y.b, Y.c from X innner join ' + @name + ' as Y on Y.DI = X.ID where X.ID = 42')

#3


0  

With dynamic query:

动态查询:

DECLARE @table AS NVARCHAR(128);
DECLARE @sql NVARCHAR(4000);

-- of course you'll have to add your WHERE clause here 
SELECT @table = TableName FROM X;
SET @sql = 'SELECT X.a, Y.b, Y.c FROM X INNER JOIN '+@table+' AS Y ON Y.ID = X.ID';

EXEC(@sql);

#1


3  

Executing

执行

select 'SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [' + X.TableName + '] AS Y ON X.ID = Y.ID 
where x.primarykey =' + x.primarykey from x

Will output a series of sql statements like

会输出一系列sql语句吗

SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [ customer ] AS Y ON X.ID = Y.ID
where x.primarykey = 1234

that you can then execute "sql to build sql" if you will.

然后您可以执行“sql来构建sql”。

#2


1  

No, that is not possible. You can't use values as table names directly in a query, and you can't join each record against a different table.

不,那是不可能的。不能在查询中直接将值用作表名,也不能将每个记录连接到不同的表。

You would have to make the join for a single record, and create the query dynamically to use a value as table name:

您必须为单个记录创建连接,并动态创建查询,以使用值作为表名:

declare @name varchar(50)
set @name = select TableName from X where ID = 42
exec('select X.a, Y.b, Y.c from X innner join ' + @name + ' as Y on Y.DI = X.ID where X.ID = 42')

#3


0  

With dynamic query:

动态查询:

DECLARE @table AS NVARCHAR(128);
DECLARE @sql NVARCHAR(4000);

-- of course you'll have to add your WHERE clause here 
SELECT @table = TableName FROM X;
SET @sql = 'SELECT X.a, Y.b, Y.c FROM X INNER JOIN '+@table+' AS Y ON Y.ID = X.ID';

EXEC(@sql);