SQL server 2005 更改数据库名称

时间:2022-07-26 21:45:45
DECLARE   @t   table
(
    
    spid 
int
)

INSERT   INTO   @t
SELECT  spid  FROM  sys.sysprocesses  AS  p
WHERE   EXISTS
(
    
SELECT   *   FROM  sys.databases
    
WHERE  name  =   ' ednchina20071202 '
    
AND  p.dbid  =  database_id
)

DECLARE   @spid   int
DECLARE   @sql   nvarchar ( max )
WHILE (( SELECT   COUNT ( * FROM   @t >   0 )
BEGIN
    
SELECT   TOP   1   @spid = spid  FROM   @t
    
SET   @sql   =   ' kill  '   +   CAST ( @spid   AS   varchar ( 100 ))

    
EXEC  sp_executesql  @sql
    
DELETE   FROM   @t   WHERE  spid  =   @spid
END

GO

EXEC  sp_dboption  ' old ' , ' single user ' , ' true '
GO
EXEC  sp_renamedb  ' old ' , ' new '
GO
EXEC  sp_dboption  ' new ' , ' single user ' , ' false '
GO