Sometimes there is a need to change all tables in the database to be owned by dbo for maintenance or to fix up accidental errors. All tables owned by dbo schema is usually best practices in the database application development with MSSQL.
The following small SQL code snippet goes through all user tables in the database and changes their owner to dbo. It usessp_changeobjectowner system stored procedure
DECLARE tabcurs CURSORFORSELECT 'dips.' + [name]FROM sysobjectsWHERE xtype = 'u'OPEN tabcursDECLARE @tname NVARCHAR(517)FETCH NEXT FROM tabcurs INTO @tnameWHILE @@fetch_status = 0BEGINEXEC sp_changeobjectowner @tname, 'dbo'FETCH NEXT FROM tabcurs INTO @tnameENDCLOSE tabcursDEALLOCATE tabcurs
Hope this is useful..
No comments:
Post a Comment