Ms SQL Server Stored Procedure batch modify user table owners

Source: Internet
Author: User

Ms SQL Server Stored Procedure batch modify user table owners

Create a login account and set login to the database to be modified.

Create storage process

Create procedure DBO. changename
@ 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

Call method: exec changename 'oldowner ', 'newowner'

Note: This method produces many stored errors.

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.