2012年11月2日星期五

sp_helpdb 出現db owner錯誤

最近常常遇到這過情況,特此記錄一下.
用sp_helpdb 時出現了錯誤


Server: Msg 515, Level 16, State 2, Procedure sp_helpdb, Line 53
Cannot insert the value NULL into column 'owner',
table 'tempdb.dbo.#spdbdesc_..._000100443608';
column does not allow nulls. INSERT fails.
The statement has been terminated.

image

經以下SQL查證後,發現很多DB的owner不見了 (變成了NULL)
大概是當時用來建DB的用戶名已經清掉

select name, suser_sname(sid) 
from master.dbo.sysdatabases
where suser_sname(sid) is null

image


用以下SQL修正

exec sp_changedbowner 'sa'
成功了!
image
如果db比較多, 可以用參考網站的sql先製成script
SELECT 'USE ' + name + '; EXEC sp_changedbowner ''sa'';' 
FROM master.dbo.sysdatabases WHERE suser_sname(sid) IS NULL
不過用sp_changedbowner 修正時出了一個小插曲
Server: Msg 15110, Level 16, State 1, Procedure sp_changedbowner, Line 46
The proposed new database owner is already a user in the database.

先把exec sp_changedbowner 'sa' 的 sa 轉為另一個admin級account,
例如exec sp_changedbowner '[domain]\administator'
運行後再運行 exec sp_changedbowner 'sa' 就回復正常.


參考網站
http://www.mssqltips.com/sqlservertip/1217/problems-running-sphelpdb-in-sql-server/

本編最新更新2012-11-02

沒有留言:

發佈留言