How to use database name dynamically in SQL Server

前端 未结 3 1981
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-28 08:18

    DECLARE @sql varchar(max)
    Declare @dbName varchar(50)='Learn'
    SET @sql='
    use '+@dbname+'
    go
    
    select TABLE_NAME  
    from INFORMATION_SCHEMA.TABLES'
    exec (@sql)
    

提交回复
热议问题