Modify the object of the SQL Server database owner

Source: Internet
Author: User
Some websites use virtual hosts. If SQL-server is used to import data.
By default, database users are the users you log on to, rather than DBO. Sometimes they are different from local users.

-- Function Description: Modify database owner objects in batches.
-- Author: Unknown
-- Usage: exec changeobjectowner 'nmkspro', 'dbo'
-- Change the object of all nmkspro owners to DBO
-- After running successfully, the system prompts: "NOTE: changing any part of the object name may damage the script and stored procedure. "
Create procedure DBO. changeobjectowner
@ Oldowner as nvarchar (128), -- original parameter owner
@ Newowner as nvarchar (128) -- New Parameter owner
As

Declare @ name as nvarchar (128)
Declare @ owner as nvarchar (128)
Declare @ ownername as nvarchar (128)

Declare curobject cursor
Select 'name' = Name,
'Owner' = user_name (UID)
From sysobjects
Where user_name (UID) = @ oldowner
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

Batch modification:
Exec sp_msforeachtable 'exec sp_changeobjectowner ''? '', ''Dbo '''
Single modification:
Exec sp_changeobjectowner 'name of the table to be changed ', 'dbo'

Method 1: Click the table-design-properties-to change the owner. This method is applicable when the number of tables is small.
Method 2: Execute the script directly, log on to the database with a system account or a super user, and then execute the following statement:
Sp_configure 'Allow updates', '1'
Go
Reconfigure with override
Go
Update sysobjects set UID = 1 where uid <> 1 -- you can modify it based on the conditions and switch between multiple users.
Go
Sp_configure 'Allow updates', '0'
Go
Reconfigure with override

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.