Thursday, 3 May 2012

Create Simple StoredProcedure in SQL SERVER

Have you ever written SQL statements, like inserts, selects, and updates? Then you have already written most of a stored procedure. A stored procedure is an already written SQL statement that is saved in the database.here is sample defines how to create a stored procedure in SQL statement

Table Example:
CREATE TABLE TBL_IEB_Expense
(
 ExpenseID INT IDENTITY(1,1) PRIMARY KEY,
 ExpenseDate DATETIME NOT NULL,
 CategoryID SMALLINT NOT NULL REFERENCES TBL_IEB_Category(CategoryID),
 Description VARCHAR(1000),
 PaymentTypeID SMALLINT NOT NULL REFERENCES TBL_IEB_PaymentType(PaymentTypeID),
 Amount DECIMAL(16,2) DEFAULT(0),
 CreatedOn DATETIME NOT NULL,
 CreatedBy INT REFERENCES TBL_IEB_User(UserID),
 ModifiedOn DATETIME
)
Stored Procedure Example:
CREATE PROCEDURE SP_SavExpan
(
  @ExpenseID INT,
 @ExpenseDate DATETIME,
 @CategoryID SMALLINT,
 @Description VARCHAR(1000),
 @PaymentTypeID SMALLINT ,
 @Amount DECIMAL(16,2),
 @CreatedOn DATETIME,
 @CreatedBy INT,
 @ModifiedOn DATETIME
 )
 AS
 BEGIN
 INSERT INTO TBL_IEB_Expense
 (ExpenseDate,CategoryID,Description,PaymentTypeID,Amount,CreatedOn,CreatedBy,ModifiedOn)
 values
 (@ExpenseDate,@CategoryID,@Description,@PaymentTypeID,@Amount,@CreatedOn,@CreatedBy,@ModifiedOn)
 END

No comments:

Post a Comment