Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all stored procedures in a specific SQL Server schema

I have hundreds of procedures auto generated by DataSync.

I don't have the time and sense to delete them manually.

They all start with DataSync.

Is there a way to delete all stored procedures where the name start with DataSync.?

like image 767
dg90 Avatar asked Feb 08 '12 10:02

dg90


People also ask

How do you clear a schema in SQL Server?

Use the DROP SCHEMA command to drop a schema. When you drop a schema, you drop all the objects defined within that schema. You cannot drop a schema if it is being accessed in other user sessions or if it is the default schema of the database.

How do you drop sp?

Delete stored procedure SQL Server management studio Open the object explorer window and navigate to Programmability > Stored Procedures under your database. Here you can see the list of stored procedures that are created in the database.

How do you Delete a table from schema?

DROP TABLE removes tables from the database. Only the table owner, the schema owner, and superuser can drop a table. To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table.


1 Answers

Use the information_schema.routines (which is fairly standard across RDBMSs such as MSSQL,Mysql):

If your proc names start "DataSync." then they are probably in a schema, so you can find them with:

select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + ']'
from 
    information_schema.routines where routine_schema = 'DataSync' and routine_type = 'PROCEDURE'

If your proc names start "DataSync" then you can find them with:

select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + ']'
from 
    information_schema.routines where routine_name like 'DataSync%' and routine_type = 'PROCEDURE'

If you wanted to execute all these drop statements, you can build a single execute using FOR XML PATH as follows:

declare @sql varchar(max)

set @sql = (
select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + '] ' 
from 
    information_schema.routines where routine_schema = 'DataSync' and routine_type = 'PROCEDURE'
FOR XML PATH ('')
)

exec (@sql)
like image 147
Jon Egerton Avatar answered Oct 04 '22 12:10

Jon Egerton