sql 模糊查询表名后再查询该表的数据

项目中有很多表,表名都是“id+2019”,我的思路是先查询表名select name  from sys.tables where name like 'id%';

再查询tablename=name这张表数据,具体实现是:

 

declare @tbname varchar(100)

select @tbname=name from sys.tables where name like 'id%';

exec('select * from '+@tbname')

 

-----------------------------------------------------------------

declare @tbname varchar(20)
select @tbname=name from sys.tables where name like '历史记录_%71_2019'
Exec('select top 100 * from ' +@tbname)

posted @ 2019-06-13 17:21  夜猫风尘  阅读(2893)  评论(0编辑  收藏  举报