How to use database name dynamically in SQL Server

前端 未结 3 1949
伪装坚强ぢ
伪装坚强ぢ 2021-01-28 07:29

I need to get all table names in SQL Server. To do this I use this code:

select TABLE_NAME  
from INFORMATION_SCHEMA.TABLES

I need use dynamic

3条回答
  •  梦毁少年i
    2021-01-28 08:32

    DECLARE @dbName varchar(50)='Learn'
    EXEC ('SELECT TABLE_NAME FROM ' +@dbName+'.INFORMATION_SCHEMA.TABLES');
    

    OR SIMPLY:

    SELECT TABLE_NAME FROM Learn.INFORMATION_SCHEMA.TABLES
    

提交回复
热议问题