SQL Server 2000 库置疑的修复


SQL Server 2000新建同名数据库,停数据库,删除文件,复制文件进来。 启动数据库,此时数据库置疑,打开查询分析器
exec sp_configure 'allow updates',1 RECONFIGURE WITH OVERRIDE /* 打开修改系统表的开关 */
update sysdatabases set status=32768 where name='BecomCQUser' /* 设置数据库状态 */
DBCC REBUILD_LOG ('BecomCQUser','D:/ClearQuest/BecomCQUser_Log.LDF') /* 重建LDF文件 */
update sysdatabases set status=0 where name='BecomCQUser' /* 重置数据库状态 */
restore database BecomCQUser WITH RECOVERY /* 恢复数据库 */
update sysdatabases set status =16 where name = 'BecomCQUser' 
exec sp_configure 'allow updates',0 RECONFIGURE WITH OVERRIDE

如果有登陆用户在登录里没有,而在该数据库里有,那么会既建不了登录,又删除不了登录用户,这时候可以改变库里对象的所有者,然后删掉用户,再新建登录即可
选中数据库,执行以下语句,把所有对象的所有者改成了dbo,之后就可以删除登录用户了
declare tb cursor local for
select 'sp_changeobjectowner ''['+replace(user_name(uid),']',']]')+'].['
+replace(name,']',']]')+']'',''dbo'''
from sysobjects
where xtype in('U','V','P','TR','FN','IF','TF') and status>=0
open tb
declare @s nvarchar(4000)
fetch tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch tb into @s
end
close tb
deallocate tb
go

相关内容