SQL 查询 SELECT FROM [来自另一个表的列的值]
我有一个表 X,当某些表发生更改时,触发器将在其中插入一行。我已将表名称插入到表 X 中。
现在,我想从表 X 中选择
数据,同时与实际表本身进行内连接
。是否可以使用 select 表的列中的值作为内连接的表?
查询应该看起来像这样
SELECT X.a, Y.b, Y.c FROM X
INNER JOIN [X.TableName] AS Y ON Y.ID = X.ID
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.
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行
将输出一系列sql语句
,您可以根据需要执行“sql to build sql”。
Executing
Will output a series of sql statements like
that you can then execute "sql to build sql" if you will.
不,那是不可能的。您不能在查询中直接使用值作为表名,也不能将每个记录与不同的表连接起来。
您必须对单个记录进行联接,并动态创建查询以使用值作为表名:
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:
使用动态查询:
With dynamic query: