Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_renamedb

  No additional text.


Syntax
create procedure sys.sp_renamedb	--- 1996/08/20 13:52
	@dbname sysname,				-- old (current) db name
	@newname sysname				-- new name we want to call it
as
	-- Use sp_rename instead.
	declare @objid int				-- object id of the thing to rename
	declare @bitdesc varchar(30)	-- bit description for the db
	declare @curdbid int			-- id of database to be changed
	declare @execstring nvarchar (max)

	--  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_renamedb')
		return (1)
	end

	--  Only the SA can do this.
	if not (is_srvrolemember('dbcreator') = 1)
	begin
		raiserror(15247,-1,-1)
		return (1)
	end

	--  Make sure the database exists.
	if not exists (select * from master.dbo.sysdatabases where name = @dbname)
	begin
		raiserror(15010,-1,-1,@dbname)
		return (1)
	end

	--  Make sure that the @newname db doesn't already exist.
	if exists (select * from master.dbo.sysdatabases where name = @newname)
	begin
		raiserror(15032,-1,-1,@newname)
		return (1)
	end

	-- Check to see that the @newname is valid.
	declare @returncode int
	EXEC @returncode = sys.sp_validname @newname
	if @returncode <> 0
	begin
		raiserror(15224,-1,15,@newname)
		return(1)
	end

	-- Don't allow the names of master, tempdb, and model to be changed.
	if @dbname in ('master', 'model', 'tempdb')
	begin
		raiserror(15227,-1,-1,@dbname)
		return (1)
	end

	select @execstring = 'ALTER DATABASE '
		+ quotename( @dbname , '[')
		+ ' MODIFY NAME = '
		+ quotename( @newname , '[')

	EXEC (@execstring)

	if @@error <>  0
	begin
		-- No need to raiserror as the CREATE DATABASE will do so
		return(1)
	end

	return (0) -- sp_renamedb

 
Last revision 2008RTM
See also

  sp_rename (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