FOR中的SQL查询动态表名称 [英] SQL query dynamic table name in FOR

查看:103
本文介绍了FOR中的SQL查询动态表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表 tbl1 ,该表的列为 tbl_names .此列包含其他一些表的名称.
现在,我要以以下格式编写查询:
select * from (select tbl_names from tbl1)

I have a table tbl1 which has a column tbl_names. This column contains the name of some other tables.
Now I want to write a query in the following format:
select * from (select tbl_names from tbl1)

我知道上面的查询将不起作用,但是如何实现呢?我是否需要编写一个存储过程或类似的东西,并在第二个查询的每个值上循环并执行第一个查询?

I know that the query above will not work but how I can achieve this? Do I need to write a stored procedure or something like that and loop on each value of second query and execute first query?

谢谢

推荐答案

您可以使用准备好的语句

You can use prepared statements

SET @a = (select tbl_names from tbl1);
SET @x := CONCAT('SELECT * FROM ', @a);
Prepare stmt FROM @x;
Execute stmt;
DEALLOCATE PREPARE stmt;

PREPARE语法

干杯.

这篇关于FOR中的SQL查询动态表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆