ALTER current database without using its name

前端 未结 3 1255
旧巷少年郎
旧巷少年郎 2021-02-18 15:52

I need to run update script on current database (ALTER DATABASE...), but can\'t use implicit its name. Is possible to use some function to get current db name and u

相关标签:
3条回答
  • 2021-02-18 16:47

    Try this

    DECLARE @DBName sysname;
    SET @DBName = (SELECT db_name());
    DECLARE @SQL varchar(1000);
    SET @SQL = 'ALTER DATABASE '+@DBName+' .......'
    

    Raj

    0 讨论(0)
  • 2021-02-18 16:49

    Actually something more like this is probably a little better if you're altering the current database:

    ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 90

    0 讨论(0)
  • 2021-02-18 16:56

    You need to use something like

    declare @dbname varchar(100)
    set @dbname=quotename(db_name())
    exec('alter database '+@dbname+' ...');
    

    or.. even simpler

    set @sql='alter database '+quotename(db_name())+' ...';
    exec(@sql)
    
    0 讨论(0)
提交回复
热议问题