不使用当前数据库的名称修改当前数据库

时间:2021-05-12 11:14:46

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 use inside ALTER DATABASE (Sql Server 2005 and above) ? I tried use db_name(), but doesn't work.

我需要在当前数据库(ALTER database…)上运行update script,但是不能使用隐式的名称。是否可以使用一些函数获取当前的db名称并在ALTER DATABASE中使用(Sql Server 2005及以上)?我尝试过使用db_name(),但是没有用。

select db_name(); works

选择db_name();作品

ALTER DATABASE db_name() ... doesn't work

改变数据库db_name()……不工作

3 个解决方案

#1


16  

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)

#2


30  

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

实际上,如果你正在修改当前的数据库,类似这样的东西可能会更好一点:

ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 90

修改数据库当前设置COMPATIBILITY_LEVEL = 90

#3


0  

Try this

试试这个

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

Raj

拉吉

#1


16  

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)

#2


30  

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

实际上,如果你正在修改当前的数据库,类似这样的东西可能会更好一点:

ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 90

修改数据库当前设置COMPATIBILITY_LEVEL = 90

#3


0  

Try this

试试这个

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

Raj

拉吉