| |

VerySource

 Forgot password?
 Register
Search
View: 2065|Reply: 5

How to delete all stored procedures prefixed with "usp_" using SQL statements

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-16 16:00:02
| Show all posts |Read mode
I want to delete all stored procedures that start with the prefix USP_. How to write DROP PROCEDURE'usp_%' This is a syntax error. How to write it?
Reply

Use magic Report

0

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-16 18:15:01
| Show all posts
Select all stored procedure names beginning with USP_ and put them into the cursor.
declare c_1 cursor for select name from sysobjects where name like'USP_%' and xtype='P'
Traverse the cursor, create dynamic SQL statements, delete stored procedures
declare @pname nvarchar(100)
open c_1
fetch next from c_1 into @pname
while @@fetch_status=0
begin
    exec ('drop PROCEDURE'+@pname)
    fetch next from c_1 into @pname
end
close c_1
deallocate c_1
Reply

Use magic Report

1

Threads

23

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-12-16 20:30:02
| Show all posts
If there are not many stored procedures after filtering
Try:
declare @str varchar(8000),@T_sql varchar(8000)
set @str=''
select @str=@str+','+name from sysobjects where xtype='P' and name like'usp_%'
select @str=stuff(@str,1,1,'')
set @T_sql='drop proc '
set @T_sql=@T_sql+replace(@str,',',' drop proc')+''
select @T_sql
exec(@T_sql)
Reply

Use magic Report

0

Threads

17

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-12-17 15:45:01
| Show all posts
declare delcursor for select name from sysobjects where name like'usp_%' and xtype='P'
declare @pname nvarchar(100)
open delcursor
fetch next from delcursor into @pname
while @@fetch_status=0
begin
    exec ('drop PROCEDURE'+@pname)
    fetch next from delcursor into @pname
end
close delcursor
deallocate delcursor
go
Reply

Use magic Report

0

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-12-19 15:30:01
| Show all posts
SELECT'DROP PROC'+name
FROM sysobjects
WHERE Name LIKE'usp_%'

--Then execute the printed result sentence by sentence
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-22 11:15:02
| Show all posts
Learned
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list