Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_dbcmptlevel

  No additional text.


Syntax
create procedure sys.sp_dbcmptlevel			-- 1997/04/15
	@dbname sysname = NULL,					-- database name to change
	@new_cmptlevel tinyint = NULL OUTPUT	-- the new compatibility level to change to
as
	set nocount    on

	declare @exec_stmt nvarchar(max)
	declare @returncode	int
	declare @comptlevel	float(8)
	declare @dbid int					-- dbid of the database
	declare @dbsid varbinary(85)		-- id of the owner of the database
	declare @orig_cmptlevel tinyint		-- original compatibility level
	declare @input_cmptlevel tinyint	-- compatibility level passed in by user
		,@cmptlvl80 tinyint				-- compatibility to SQL Server Version 8.0
		,@cmptlvl90 tinyint				-- compatibility to SQL Server Version 9.0
		,@cmptlvl100 tinyint				-- compatibility to SQL Server Version 10.0
	select  @cmptlvl80 = 80,
			@cmptlvl90 = 90,
			@cmptlvl100 = 100

	-- SP MUST BE CALLED AT ADHOC LEVEL 
	if (@@nestlevel > 1)
	begin
		raiserror(15432,-1,-1,'sys.sp_dbcmptlevel')
		return (1)
	end

	-- If no @dbname given, just list the valid compatibility level values.
	if @dbname is null
	begin
	   raiserror (15048, -1, -1, @cmptlvl80, @cmptlvl90, @cmptlvl100)
	   return (0)
	end

	--  Verify the database name and get info
	select @dbid = dbid, @dbsid = sid ,@orig_cmptlevel = cmptlevel
		from master.dbo.sysdatabases
		where name = @dbname

	--  If @dbname not found, say so and list the databases.
	if @dbid is null
	begin
		raiserror(15010,-1,-1,@dbname)
		print ' '
		select name as 'Available databases:'
			from master.dbo.sysdatabases
		return (1)
	end

	-- Now save the input compatibility level and initialize the return clevel
	-- to be the current clevel
	select @input_cmptlevel = @new_cmptlevel
	select @new_cmptlevel = @orig_cmptlevel

	-- If no clevel was supplied, display and output current level.
	if @input_cmptlevel is null
	begin
		raiserror(15054, -1, -1, @orig_cmptlevel)
		return(0)
	end

	-- If invalid clevel given, print usage and return error code
	-- 'usage: sp_dbcmptlevel [dbname [, compatibilitylevel]]'
	if @input_cmptlevel not in (@cmptlvl80, @cmptlvl90, @cmptlvl100)
	begin
		raiserror(15416, -1, -1)
		print ' '
		raiserror (15048, -1, -1, @cmptlvl80, @cmptlvl90, @cmptlvl100)
		return (1)
	end

	--  Only the SA or the dbo of @dbname can execute the update part
	--  of this procedure sys.so check.
	if (not (is_srvrolemember('sysadmin') = 1)) and suser_sid() <> @dbsid
		-- ALSO ALLOW db_owner ONLY IF DB REQUESTED IS CURRENT DB
		and (@dbid <> db_id() or is_member('db_owner') <> 1)
	begin
		raiserror(15418,-1,-1)
		return (1)
	end

	-- If we're in a transaction, disallow this since it might make recovery impossible.
	set implicit_transactions off
	if @@trancount > 0
	begin
		raiserror(15002,-1,-1,'sys.sp_dbcmptlevel')
		return (1)
	end

	set @exec_stmt = 'ALTER DATABASE ' + quotename(@dbname, '[') + ' SET COMPATIBILITY_LEVEL = ' + cast(@input_cmptlevel as nvarchar(128))

	-- Note: database @dbname may not exist anymore
	exec(@exec_stmt)

	select @new_cmptlevel = @input_cmptlevel

	return (0) -- sp_dbcmptlevel

 
Last revision 2008RTM
See also

  sp_instdist (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash