5 Aug 2024

Naming conventions for SQL Server stored procedures

 

Overview

One good thing to do for all of your SQL Server objects is to come up with a naming convention to use.  There are not any hard and fast rules, so this is really just a guideline on what should be done.

Explanation

SQL Server uses object names and schema names to find a particular object that it needs to work with.  This could be a table, stored procedure, function ,etc...

It is a good practice to come up with a standard naming convention for you objects including stored procedures.


Do not use sp_ as a prefix

One of the things you do not want to use as a standard is "sp_".  This is a standard naming convention that is used in the master database.  If you do not specify the database where the object is, SQL Server will first search the master database to see if the object exists there and then it will search the user database. So avoid using this as a naming convention.


Standardize on a Prefix

It is a good idea to come up with a standard prefix to use for your stored procedures.  As mentioned above do not use "sp_", so here are some other options.

  • usp_
  • sp
  • usp
  • etc...

To be honest it does not really matter what you use.  SQL Server will figure out that it is a stored procedure, but it is helpful to differentiate the objects, so it is easier to manage.

So a few examples could be:

  • spInsertPerson
  • uspInsertPerson
  • usp_InsertPerson
  • InsertPerson

Again this is totally up to you, but some standard is better than none.


Naming Stored Procedure Action

I liked to first give the action that the stored procedure takes and then give it a name representing the object it will affect.

So based on the actions that you may take with a stored procedure, you may use:

  • Insert
  • Delete
  • Update
  • Select
  • Get
  • Validate
  • etc...

So here are a few examples:

  • uspInsertPerson
  • uspGetPerson
  • spValidatePerson
  • SelectPerson
  • etc...

Another option is to put the object name first and the action second, this way all of the stored procedures for an object will be together.

  • uspPersonInsert
  • uspPersonDelete
  • uspPersonGet
  • etc...

Again, this does not really matter what action words that you use, but this will be helpful to classify the behavior characteristics.


Naming Stored Procedure Object

The last part of this is the object that you are working with.  Some of these may be real objects like tables, but others may be business processes.  Keep the names simple, but meaningful.  As your database grows and you add more and more objects you will be glad that you created some standards.

So some of these may be:

  • uspInsertPerson - insert a new person record
  • uspGetAccountBalance - get the balance of an account
  • uspGetOrderHistory - return list of orders

Schema Names

Another thing to consider is the schema that you will use when saving the objects.  A schema is the a collection of objects, so basically just a container.  This is useful if you want to keep all utility like objects together or have some objects that are HR related, etc...

This logical grouping will help you differentiate the objects further and allow you to focus on a group of objects.

Here are some examples of using a schema:

  • HR.uspGetPerson
  • HR.uspInsertPerson
  • UTIL.uspGet
  • UTIL.uspGetLastBackupDate
  • etc...

To create a new schema you use the CREATE SCHEMA command

Here is a simple example to create a new schema called "HR" and giving authorization to this schema to "DBO".

CREATE SCHEMA [HumanResources] AUTHORIZATION [dbo]

Putting It All Together

So you basically have four parts that you should consider when you come up with a naming convention:

  • Schema
  • Prefix
  • Action
  • Object

Take the time to think through what makes the most sense and try to stick to your conventions.

4 Aug 2024

Using comments in a SQL Server stored procedure

 

Overview

One very helpful thing to do with your stored procedures is to add comments to your code.  This helps you to know what was done and why for future reference, but also helps other DBAs or developers that may need to make modifications to the code.

Explanation

SQL Server offers two types of comments in a stored procedure; line comments and block comments.   The following examples show you how to add comments using both techniques.  Comments are displayed in green in a SQL Server query window.

Line Comments

To create line comments you just use two dashes "--" in front of the code you want to comment.  You can comment out one or multiple lines with this technique.

In this example the entire line is commented out.

-- this procedure gets a list of addresses based 
-- on the city value that is passed
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City
GO

This next example shows you how to put the comment on the same line.

-- this procedure gets a list of addresses based on the city value that is passed
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City -- the @City parameter value will narrow the search criteria
GO

Block Comments

To create block comments the block is started with "/*" and ends with "*/".   Anything within that block will be a comment section.

/* 
-this procedure gets a list of addresses based 
 on the city value that is passed
-this procedure is used by the HR system      
*/
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City
GO

Combining Line and Block Comments

You can also use both types of comments within a stored procedure.


/* 
-this procedure gets a list of addresses based 
 on the city value that is passed
-this procedure is used by the HR system      
*/
CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM Person.Address
WHERE City = @City -- the @City parameter value will narrow the search criteria
GO

Union Budget 2024-25 – All You Need to Know

  Union Budget 2024-25 – All You Need to Know The Union Budget is a yearly financial plan presented by the Finance Minister of India for the...