Home Database Mysql Tutorial sqlserver 修改数据库表所有者

sqlserver 修改数据库表所有者

Jun 07, 2016 pm 03:40 PM
exe sqlserver Revise owner batch database

批量修改: EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' ' 单个修改: exec sp_changeobjectowner '要改的表名','dbo' 只有所有者才能更改表的所有者 有很服务器: 消息 15001,级别 16,状态 1,过程 sp_changeobjectowner,行 38 对

批量修改:
EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' '
单个修改:
exec sp_changeobjectowner '要改的表名','dbo'

只有所有者才能更改表的所有者


有很服务器:   消息   15001,级别   16,状态   1,过程   sp_changeobjectowner,行   38   
  对象   'user'   不存在或不是对此操作有效的对象。   
多表是导入的 一些所有者的密码都不记得就会产生
觉得是不是没有原来的所有者 我加了一下 嘿嘿 正常!
exec sp_changeobjectowner '表所有者.要改的表名','dbo'
就不需表的所有者才能修改了

 

 

 

补充:

--执行这个语句,就可以把当前库的所有表的所有者改为dbo
exec sp_msforeachtable 'sp_changeobjectowner ''?'', ''dbo'''


--如果是要用户表/存储过程/视图/触发器/自定义函数一齐改,则用游标(不要理会错误提示)
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

1. sp_changeobjectowner
更改当前数据库中对象的所有者。

语法
sp_changeobjectowner [ @objname = ] 'object' , [ @newowner = ] 'owner'

参数
[@objname =] 'object'

当前数据库中现有的表、视图或存储过程的名称。object 的数据类型为 nvarchar(517),没有默认值。object 可用现有对象所有者限定,格式为 existing_owner.object。

[@newowner =] 'owner'

即将成为对象的新所有者的安全帐户的名称。owner 的数据类型为 sysname,没有默认值。owner 必须是当前数据库中有效的 Microsoft? SQL Server? 用户或角色或 Microsoft Windows NT? 用户或组。指定 Windows NT 用户或组时,请指定 Windows NT 用户或组在数据库中已知的名称(用 sp_grantdbaccess 添加)。

返回代码值
0(成功)或 1(失败)

注释
对象所有者(或拥有对象的组或角色的成员)对对象有特殊的权限。对象所有者可以执行任何与对象有关的 Transact-SQL 语句(例如 Insert、Update、Delete、Select 或 EXECUTE),也可以管理对象的权限。

如果拥有对象的安全帐户必须要除去,但同时要保留该对象,请使用 sp_changeobjectowner 更改对象所有者。该过程从对象中删除所有现有权限。在运行 sp_changeobjectowner 之后,需要重新应用要保留的任何权限。

由于这个原因,建议在运行 sp_changeobjectowner 之前,编写现有权限的脚本。一旦更改了对象的所有权,可能要使用该脚本重新应用权限。在运行该脚本之前需要在权限脚本中修改对象所有者。有关编写数据库脚本的更多信息,请参见编写数据库文档和脚本。

可以使用 sp_changedbowner 更改数据库的所有者。

权限
只有 sysadmin 固定服务器角色和 db_owner 固定数据库角色成员,或既是 db_ddladmin 固定数据库角色又是 db_securityadmin 固定数据库角色的成员,才能执行 sp_changeobjectowner。

示例
下面的示例将 authors 表的所有者改为 Corporate\GeorgeW。

EXEC sp_changeobjectowner 'authors', 'Corporate\GeorgeW'


请参见
改变数据库所有者:sp_changedbowner

------------------------------以上摘自sql server 的联机丛书
注意:
使用sqlserver2000改变对象(object)(如Table,SP,View)的所有者(owner)时,需要注意一点:
如果对象改变前的owner为dbo,则直接用:exec sp_changeobjectowner 对象名,新的所有者名就可以成功。如果再需要改变该对象的所有者,则要用如下语法:
exec sp_changeobjectowner "[所有者].[对象名]",新的所有者。注意,双引号不可省略,否则提示objectname does not exist。

EXEC sp_changeobjectowner 'cqadmin.authors', 111

 

2.建与原表结构相同表,然后把数据从原表插入到新表,操作方法:

在sql server企业管理器中选择原表点右键,选择“所有任务/生存SQL脚本”,保存生成的脚本,在脚本中把表的原所有者改成新所有者。然后再把该脚本运行一遍,数据库中就新生成了一个表,所有者是新所有者,原来的表还在数据库中没有被替换。新表和旧表 表名相同,所有者不同,此时的新表中没有数据。

然后再运行下面的语句往新表中插入数据:

Insert INTO CQAdmin.m_actiondef //新所有者.新表名
Select *
FROM RegaltecDefSchema. CQAdministrator.m_actiondef // 数据库名.旧所有者.旧表名
运行完了就做完了,可以把旧表删掉,也可以留着。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

PHP connections to different databases: MySQL, PostgreSQL, Oracle and more PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles