ALTER DATABASE CURRENT - exception

2019-07-27 17:55发布

问题:

I have a stored procedure where i change DB properties . It uses ALTER DATABASE CURRENT in the script. But i found that this works fine in 2012 MSSQL version , but gives an exception in the older version. How to solve this problem ?

sample Script content:

  ALTER DATABASE CURRENT SET ANSI_PADDING OFF 
  GO

Error:

 Incorrect syntax near the keyword CURRENT

回答1:

You can use anonymous block with sp_executesql -

declare @db_name varchar(64) 
declare @stat_sql nvarchar(256) 
begin   
  select @db_name = (select DB_NAME())
  set @stat_sql = N'ALTER DATABASE ' + @db_name + ' SET ANSI_PADDING OFF'
  EXECUTE sp_executesql @stat_sql
end