SQL Server中的sp_resetstatus

如果自己搞不定可以找诗檀软件专业SQL SERVER数据库修复团队成员帮您恢复!

诗檀软件专业数据库修复团队

服务热线 : 13764045638   QQ号:47079569    邮箱:[email protected]

 

sp_resetstatus 这个系统存储过程用以重置数据库的置疑(suspect )状态。语法是:

sp_resetstatus [ @dbname = ] ‘database’

 

sp_resetstatus用来将数据库的置疑状态位重置,其更新sys.databases中的系统字典记录。官方推荐在错误日志中的信息被充分考虑的情况下才去做这个操作。同时建议在执行sp_resetstatus后重启SQL SERVER服务实例。

一个数据库进入置疑状态的原因可能有很多;例如数据库原本能够访问的操作系统资源突然变得不可用,或者数据库MDF文件出现讹误等。

以下是一个例子: EXEC sp_resetstatus ‘AdventureWorks2012’;

在SYBASE的文档中记录了该存储过程的代码,有理由相信SQL SERVER中其代码的主体作用应当是相似的:

 

CREATE PROC sp_resetstatus @dbname varchar(30) AS
DECLARE @msg varchar(80)
IF @@trancount > 0
    BEGIN
      PRINT "Can't run sp_resetstatus from within a transaction."
      RETURN (1)
    END
IF suser_id() != 1
    BEGIN
     SELECT @msg =  "You must be the System Administrator (SA)"
     SELECT @msg = @msg + " to execute this procedure."
     PRINT @msg
     RETURN (1)
    END
IF (SELECT COUNT(*) FROM master..sysdatabases
     WHERE name = @dbname) != 1
    BEGIN
     SELECT @msg = "Database '" + @dbname + "' does not exist!"
     PRINT @msg
     RETURN (1)
    END
IF (SELECT COUNT(*) FROM master..sysdatabases
     WHERE name = @dbname AND status & 256 = 256) != 1
   BEGIN
      PRINT "sp_resetstatus may only be run on suspect databases."
      RETURN (1)
    END
BEGIN TRAN
   UPDATE master..sysdatabases SET status = status - 320
     WHERE name = @dbname
   IF @@error != 0 OR @@rowcount != 1
     ROLLBACK TRAN
   ELSE 
       BEGIN
         COMMIT TRAN
         SELECT @msg = "Database '" + @dbname + "' status reset!"
         PRINT @msg
         PRINT " " 
         PRINT "WARNING: You must reboot Adaptive Server prior to  "
         PRINT "          accessing this database!"
         PRINT " "
       END

 

 


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *