About Stored Procedures in Microsoft SQL Server

 


Before discussing about different types of stored procedures, first we have to know what stored procedure is. A stored procedure is a set of Structured Query Language (SQL) statements that you assign a name to and store in a database in compiled form so that you can share it between a number of programs.

 

    • They allow modular programming.
    • They allow faster execution.
    • They can reduce network traffic.
    • They can be used as a security mechanism.

 

Different types of Stored Procedures:

There are four types’ stored procedures. Those are

 

Temporary Stored Procedures:

SQL Server supports two types of temporary procedures: local and global. A local temporary procedure is visible only to the connection that created it. A global temporary procedure is available to all connections. Local temporary procedures are automatically dropped at the end of the current session. Global temporary procedures are dropped at the end of the last session using the procedure. Usually, this is when the session that created the procedure ends. Temporary procedures named with # and ## can be created by any user.

 

System stored procedures:

System stored procedures are created and stored in the master database and have the sp_ prefix.(or xp_) System stored procedures can be executed from any database without having to qualify the stored procedure name fully using the database name master. (If any user-created stored procedure has the same name as a system stored procedure, the user-created stored procedure will never be executed)

 

Automatically Executing Stored Procedures:

One or more stored procedures can execute automatically when SQL Server starts. The stored procedures must be created by the system administrator and executed under the sysadmin fixed server role as a background process. The procedure(s) cannot have any input parameters.

 

User stored procedure:

User stored procedures are procedures created by user with our own functionality.

 

Automatic execution of stored procedure:
We can use the sp_procoption system stored procedure to mark the stored procedure to automatic execution when the SQL Server will start. Only objects in the master database owned by dbo can have the startup setting changed and this option is restricted to objects that have no parameters.

 

USE master
EXEC sp_procoption 'indRebuild', 'startup', 'true')

 

When used a stored procedure, the RETURN statement can specify an integer value to return to the calling application, batch, or procedure. If no value is specified on RETURN, a stored procedure returns the value 0.  The stored procedures return a value of 0 when no errors were encountered. Any nonzero value indicates an error occurred.

 

Do not create any stored procedures using sp_ as a prefix. Because SQL Server always looks for a stored procedure beginning with sp_ in this order:

 

    The stored procedure in the master database.

 

    The stored procedure based on any qualifiers provided (database name or     owner).

 

    The stored procedure using dbo as the owner, if one is not specified.

 

Therefore, although the user-created stored procedure prefixed with sp_ may exist in the current database, the master database is always checked first, even if the stored procedure is qualified with the database name.

 

SQL Server provides three ways to recompile a stored procedure:

    • The sp_recompile system stored procedure forces a recompile of a stored procedure the next time it is run.
    • Creating a stored procedure that specifies the WITH RECOMPILE option in its definition indicates that SQL Server does not cache a plan for this stored procedure; the stored procedure is recompiled each time it is executed. Use the WITH RECOMPILE option when stored procedures take parameters whose values differ widely between executions of the stored procedure, resulting in different execution plans to be created each time. Use of this option is uncommon, and causes the stored procedure to execute more slowly because the stored procedure must be recompiled each time it is executed.
    • You can force the stored procedure to be recompiled by specifying the WITH RECOMPILE option when you execute the stored procedure. Use this option only if the parameter you are supplying is atypical or if the data has significantly changed since the stored procedure was created.

 

You can use the FOR XML clause of the SELECT statement to return XML as output from stored procedures, and within the FOR XML clause you specify an XML mode: RAW, AUTO, or EXPLICIT.

 

 

Difference between view and stored procedure:
Views can have only select statements (create, update, truncate, delete statements are not allowed) Views cannot have “select into”, “Group by” “Having”, ”Order by”

 

Difference between a function and a stored procedure: 

  • Functions can be used in a select statement where as procedures cannot.
  • Procedure takes both input and output parameters but Functions takes only input parameters.
  • Functions cannot return values of type text, ntext, image & timestamps where as procedures can.
  • Functions can be used as user defined datatypes in create table but procedures cannot.