SQL server 2005 更改数据库名称
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
(
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
posted on 2007-12-03 17:09 Goodspeed 阅读(1109) 评论(0) 编辑 收藏 举报