Don’t start your user defined stored procs with SP_

There is a common practice with some development teams that they prefix any user defined stored procs with “sp_”. It’s a bad practice in a way that it was a recipe for disaster when you strike the bulb.

What I mean is that, the system stored procs in SQL Server are named starting with “sp_”. Now, when you try to name your user-defined stored procs starting with “sp_” what happens is that in addition to bringing down the performance it might give you a “gotcha” moment.

Here is how SQL Server looks for a stored proc starting with “sp_” in this hierarchy of databases,

  1. Master DB
  2. The stored procedure based on the fully qualified name provided
  3. The stored procedure using dbo as the owner, if one is not specified

So, when you’ve named your user-defined stored proc starting with “sp_” in a database other than Master database, the Master database is always checked first when you explicitly don’t specify the database name and so when you’ve accidentally named your user-defined stored proc similar in name as the system stored proc then the user-defined stored proc will never get executed.

For example, let’s say you’ve accidentally named your user-defined stored proc as “sp_help” with the definition,

create proc sp_help
as
select * from dbo.login

Now, when you try to execute the above created proc using the

Exec sp_help

you’d be surprised to see that the sp_help under Master database has got executed than from your database.

To deal with this it is a best practice to use “usp_” rather than “sp_”. At least that’s what I prefer to use.

Technorati Tags: ,,
Related Posts with Thumbnails


Subscribe to our free RSS feed to keep up with what's happening at DemoGeek.com

At DemoGeek.com we try to explain the tech stuffs in as much detail as we can to help the not-so-tech-savvy of our friends understand and follow the tech stuffs.

Share/Save/Bookmark

Subscribe to DemoGeek.com

No comments yet

Leave a Reply





XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About DemoGeek.com

DemoGeek.com brings you high quality articles, tutorials and how to's on everything related to Microsoft Windows, Mac OSX, iPhone, Useful Websites, Browsers, Software and Programming.

At DemoGeek.com you'll find step-by-step explanation of the technical issues explained in detail with screenshots and screencasts. Read More...

Contact |  FUG |  Disclaimer

Creative Commons License

Canonical URL by SEO No Duplicate WordPress Plugin