SQL Server:如何从动态多个表中获取数据?
使用 SQL 服务器。
数据库包含来自不同年份的数据,我想以某种方式获取所有数据(所有年份)并向用户显示,例如,数据库包含表:
table
--------
records_2000_01
records_2000_02
records_2000_03
...
现在通过 select TABLE_NAME into @tableName from information_schema.tables where table_name like 'records_%'
我可以获取所有表名,如何编写 SQL(或者可能是过程)来从这些表中获取所有数据?将所有记录放入一张表中?
谢谢。
Using SQL SERVER.
The database contains data from different years, somehow I want to fetch all the data (all the years) and show to the user, for example, database contains table:
table
--------
records_2000_01
records_2000_02
records_2000_03
...
now through
select TABLE_NAME into @tableName from information_schema.tables where table_name like 'records_%'
I can fetch all the table name, how to write a SQL (or perhaps procedure) to fetch all data from these tables? make all record into one table?
thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此创建过程,您需要遵循以下步骤。
创建表Common_Table
,其结构与您的所有表(例如records_2000_02 等)相同。' Insert into your Common_Table select * from ' + Table_name
从 Information_schema 获取。Create procedure in this Below steps you need to follow.
Create table Common_Table
with same one structure which all your table like records_2000_02 and others.' Insert into your Common_Table select * from ' + Table_name
that you get from Information_schema.