Batch delete stored procedures and batch modify stored procedures

Source: Internet
Author: User

Batch delete stored procedures and batch modify stored procedures
Modify:

Declare proccur cursor
For
Select [name] from sysobjects where name like 'Foods _ %'
Declare @ procname varchar (100)
Declare @ temp varchar (100)
Open proccur
Fetch next from proccur into @ procname
While (@ FETCH_STATUS = 0)
Begin
Set @ temp = 'kcb _ '+ @ procname
EXEC SP_RENAME @ procname, @ temp
  
Print (@ procname + 'deleted ')
Fetch next from proccur into @ procname
End
Close proccur
Deallocate proccur

Detailed instance

Modify the table owner in batches:

EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''? '', ''Dbo '''
Modify the table owner:
Exec sp_changeobjectowner 'name of the table to be changed ', 'dbo'

Modify the stored procedure in batches:

Create procedure ChangeProcOwner @ OldOwner as NVARCHAR (128), -- Parameter original owner @ NewOwner as NVARCHAR (128) -- Parameter new owner 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 execute exec ChangeProcOwner 'XX ', 'dbo'
Or
Exec ChangeProcOwner '? ', 'Dbo'

Another method is as follows:

-- If a database tutorial (table and stored procedure) has multiple user names, you can use the following statement to change all of them to dbo:

Select 'sp _ changeobjectowner ''' + User_Name (Uid) + '. '+ name + ''', ''dbo'' From sysobjects Where Uid Not in (User_ID ('dbo') And Type In ('u', 'P ')

Delete:

First look at the deleted

Sqlserver 2005 can only delete one stored procedure at a time. If there is more than one stored procedure, it takes a long time to delete it. Therefore, a statement is written, simply drop all user-defined stored procedures in the database. However, please pay attention to the database currently opened. Post code below

Declare proccur cursor
For
Select [name] from sysobjects where type = 'p'
Declare @ procname varchar (100)
Open proccur
Fetch next from proccur into @ procname
While (@ FETCH_STATUS = 0)
Begin
-- Exec ('drop proc' + @ procname) -- This sentence is commented out, please cancel

Print (@ procname + 'deleted ')
Fetch next from proccur into @ procname
End
Close proccur
Deallocate proccur

 

Let's look at the batch delete stored procedure.

Declare proccur cursor
For
Select [name] from sysobjects where name like 'users _ %'
Declare @ procname varchar (100)
Open proccur
Fetch next from proccur into @ procname
While (@ FETCH_STATUS = 0)
Begin
Exec ('drop proc' + @ procname)
Print (@ procname + 'deleted ')
Fetch next from proccur into @ procname
End
Close proccur
Deallocate proccur

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.