1

I need to run a script like

EXEC sp_dbcmptlevel AdventureWorks, 100;
GO

Now I'd like to have a generic version that uses the database I am connected to, is it possible in T-SQL?

Something like this, but this doesn't work:

EXEC sp_dbcmptlevel DBNAME(), 100;
GO

It doesn't work of course because a string is returned, I'd like to have the database referece returned... Is it possible?

Note Of course I can do it from a client application and using a parameter, like (Delphi example)

EXEC sp_dbcmptlevel :CurrentDatabase, 100;
GO

and replace it at runtime, but I'd like to have T-SQL only code.

2 Answers 2

3

You can use the EXEC approach.

declare @x nvarchar(2000)
set @x = 'alter database [' + DB_NAME() + '] set compatibility_level = 100;'
exec (@x)

BOL 2008 declares that sp_dbcmptlevel is deprecated, and to use ALTER DATABASE instead. I don't know whether 2005 supports this or not.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot, a GREAT ANSWER FULL OF INFORMATION. It solved my problem and made me avoid the use of a deprecated feature.
Doesn't work on SQL2005. Just use the above EXEC approach with sp_dbcmptlevel instead.
Tried to use the above EXEC approach with sp_dbcmptlevel instead, and got: Stored procedure 'sys.sp_dbcmptlevel' can only be executed at the ad hoc level :(
Hi @Greg Woods - have you tried (replace [at] with @ symbol): declare [at]currentDbName sysname; set [at]currentDbName = db_name(); exec sys.sp_dbcmptlevel [at]dbname = [at]currentDbName, [at]new_cmptlevel = 80;
Thanks for making me look at this again, I'd missed the 'exec'. Doh!
1

You can try

DECLARE @dbname AS nvarchar(500)
set @dbname = db_name()
exec sp_dbcmptlevel @dbname, 100;

Your server must of'course support the compatibility level you try to set ..

You should also have a look at this http://msdn.microsoft.com/en-us/library/ms178653.aspx

2 Comments

Sorry, doesn't work. Incorrect syntax near 'sp_dbcmptlevel'.
My mistake, I'd missed out the 'exec' at the start of the line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.