Batch modify the Owner in SQL ...

xiaoxiao2021-03-06  60

In the process of work, you often have a problem that you need to modify the Owner of Table. If a diameter change will be taken, the following statement can be modified in sequence:

Declare @t_name varchar (100), @ T_OWNER VARCHAR (50), @ SQL VARCHAR (500), @ T_New Varchar (50) Set @ T_OLD = 'DBO'SET @ T_New =' Ivuser'Declare t_owner CURSOR FORSELECT [NAME], User_NAME (uid) AS UserName FROM SYSOBJECTS WHERE xTYPE <> 'S'OPEN t_ownerFETCH NEXT FROM t_owner INTO @ T_Name, @ T_OwnerWHILE @@ FETCH_STATUS = 0BEGIN IF UPPER (@T_Owner) = UPPER (@T_Old) Begin set @ SQL = 'sp_changeObjectowner' '' @ t_old '', '' ' @ T_New ' '' --Print @SQL EXEC (@SQL) end fetch from t_owner into @ T_name, @ T_OWNERENDCLOSE T_OWNERDEAllocate T_OWNER

---------------------------------------------------------------------------------------------------------------------------------------

Among them, @t_old is old Owner, @ T_New is a new Owner to modify.

转载请注明原文地址:https://www.9cbs.com/read-114623.html

New Post(0)