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

  • 相关阅读:
    CDH执行spark任务
    CDH日志目录
    CDH5.12安装遇到的坑
    Scala 运行jar命令
    二进制
    build 文件夹中的 webpack.prod.conf.js
    build 文件夹中的 check-versions.js
    build 文件夹中的 webpack.prod.conf.js
    build 文件夹中的 webpack.dll.config.js
    build 文件夹中的 webpack.dev.conf.js
  • 原文地址:https://www.cnblogs.com/huangtailang/p/2251279.html
Copyright © 2011-2022 走看看