Restrict SP_ (Naming convention) using DDL Trigger

Sometimes naming convention also impacts the performance problems in SQL SERVER. Whenever we create stored procedures, we are giving the name just like SP_Get_Customer. SP_ it first checks the Master Database system procedures.

All the system procedures are stored in the Master Database. In this blog we do not allow SP_ whenever stored procedure creating by using DDL Trigger.

In many articles I read the naming convention problems, SP_ really impact performance in SQL-SERVER. Personally, I also faced the performance based situations. In the beginning stage I also used SP_, later I realized that the performance problems are also important in SQL.

Today I come up with this problem by solving the by DDL Triggers. DDL Triggers introduced in SQL –SERVER 2005 version onwards. These triggers can fired whenever any Data Definition operations can be performed.

These Triggers can be performed at either the database level or server level. In this article I used database level triggers.

We will discuss Triggers in later articles.
  1. --Create Student Table  
  2. Create table Student  
  3. (               
  4.                 id int identity(1,1) ,  
  5.                 Name varchar(100)  
  6. )  
  7.   
  8. --Create DDL Trigger on Database  
  9. create  trigger Restrict_Sp_NameConvenction  
  10. on database  
  11. for create_Procedure  
  12. as  
  13. begin  
  14. declare @EventData XML = EVENTDATA(),@ObjectName Nvarchar(100);  
  15. select    @ObjectName=@EventData.value('(/EVENT_INSTANCE/ObjectName)[1]','NVARCHAR(100)');  
  16.                 if(substring(@ObjectName,1,3)='sp_')  
  17.                 begin  
  18.                                 raiserror('Procedure name can not be start with sp_',16,1)  
  19.                                 rollback;  
  20.                 end  
  21. end  
  22.   
  23. --Create Stored Procedure Name starts with sp_  
  24. create procedure sp_Get_Student  
  25.  as  
  26.  begin  
  27.                 select * from Student  
  28.  end  
Msg 50000, Level 16, State 1, Procedure Restrict_Sp_NameConvenction, Line 10
Procedure name can not be start with sp_
Msg 3609, Level 16, State 2, Procedure sp_Get_Student, Line 1
The transaction ended in the trigger. The batch has been aborted.

  1. --Create Stored Procedure Name does not starts with sp_  
  2. create procedure usp_Get_Student  
  3.  as  
  4.  begin  
  5.                 select * from Student  
  6.  end  
Command(s) completed successfully.
  1.  drop procedure sp_Get_Student  
  2.   
  3. -- Disable trigger from Databse  
  4. disable trigger Restrict_Sp_NameConvenction on database  
  5.   
  6. --Enable trigger from Databse  
  7. Enable trigger Restrict_Sp_NameConvenction on database  
  8.   
  9. --Drop trigger from Databse  
  10. drop trigger Restrict_Sp_NameConvenction on database  
  11.   
  12. --For more information about trigger event types  
  13.  select * from sys.trigger_event_types