zoukankan      html  css  js  c++  java
  • SqlServer修改表的所有者为“dbo”

    更改表的所有者

    所有:exec sp_msforeachtable 'sp_changeobjectowner ''?'', ''dbo'''

    单个:exec   sp_changeobjectowner   'city','user' --city表名

    更改表的存储过程所有者

    CREATE PROCEDURE ChangeProcOwner
    @OldOwner as NVARCHAR(128),--参数原所有者
    @NewOwner as NVARCHAR(128)--参数新所有者
    AS
    DECLARE @Name as NVARCHAR(128)
    DECLARE @Owner as NVARCHAR(128)
    DECLARE @OwnerName as NVARCHAR(128)
    DECLARE curObject CURSOR FOR
    select 'Name' = name,
    'Owner' = user_name(uid)
    from sysobjects
    where user_name(uid)=@OldOwner and xtype='p'
    order by name
    OPEN curObject
    FETCH NEXT FROM curObject INTO @Name, @Owner
    WHILE(@@FETCH_STATUS=0)
    BEGIN
    if @Owner=@OldOwner
    begin
    set @OwnerName = @OldOwner + '.' + rtrim(@Name)
    exec sp_changeobjectowner @OwnerName, @NewOwner
    end
    FETCH NEXT FROM curObject INTO @Name, @Owner
    END
     close curObject
    deallocate curObject
    GO

    执行方法:

    exec ChangeProcOwner 'tickets_f','dbo' --"tickets_f"为原所有者,dbo为新所有者
    或者
    exec ChangeProcOwner '?','tickets_f'

    --修改表,视图,存储过程 所有者方法

    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

  • 相关阅读:
    夜神 虚拟机调试 HBuilder 移动端应用
    maven filter 文件分环境打包部署小问题
    fatal: remote error: CAPTCHA required
    程序员今年是这样计划的
    线程池shutdown与shutdownnow 小例子
    JAVA知识点脉络记忆-刻意练习
    日志
    (职员)2015-12-02 星期三 日志
    (职员)2015-12-04 星期五 周志
    (职员)2015-12-04 星期五 日志
  • 原文地址:https://www.cnblogs.com/huangtailang/p/2251279.html
Copyright © 2011-2022 走看看