SQL Server Stored Procedure

INTRODUCTION TO STORED PROCEDURES

  1. Reusable code module stored in database
  2. Contain T-SQL that perform operations on data using DML or objects using DDL
  3. Contain logic error handling statements
  4. Can accept parameters-Input parameters, output parameters
  5. Return multiple results
  6. 3 types of stored procedures-user define, extended & system
  7. Recompile if change in schema or in table structure
  8. DROP PROCEDURE { [ schema_name. ] procedure } [ ,...n ] like

    DROP PROCEDURE dbo.usp_GetCategory</p>

Benefits

  1. Fast, pre-complied & stored in databases ( as long as restart sql server )
  2. Reduce network traffic
  3. Enhance security
  4. Modular programming can save time & maintainability
  5. Can be encrypted
  6. Instead, stored procedures are executed as follows: Exec sp_helpdb;

Syntex

CREATE { PROC | PROCEDURE } [schema_name.]procedure_name [ ; number ]
  [ { @parameter [ schema_name.]data_type }
  [ VARYING ] [ = default ] [ OUT | OUTPUT ]
  ] [ ,...n ]
  [ WITH { [ ENCRYPTION ]
  , [ RECOMPILE ]
  , [ EXECUTE_AS_Clause ]
  [ ,...n ] ]
  [ FOR REPLICATION ]
  SQL_Statements
[ RETURN scalar_expression ]

Basic Stored Procedure Example

CREATE/ALTER  PROCEDURE [dbo].[AllContents]
AS
BEGIN
		select * from contents
END
GO

Tags:

Leave a Reply