How to Write a Stored Procedure in SQL Server

BEFORE WRITING STORE PROCEDURE YOU HAVE TO CREAT TABLE IN SQL SERVER AFTER THAT WE CAN CREATE STORE PROCEDURE.

SUPPOSE WE HAVE ONE TABLE TBL_REGISTRATION. 

TABLE STRUCTURE

 

ID NAME ADRS
1 MOHIT DELHI
2 SANDY FARIDABAD

 

Now we  start ,how to write store procedure.......First we use Create procedure staement

Syantax:

Create Procedure Procedure-name 
(
Input parameters ,
Output Parameters (If required)
)
As
Begin
     Sql statement used in the stored procedure
End

 

Example:

 

Now we want to return NAme from registration table where ID is 2.

 

Create  PROCEDURE Getname
( @sid INT --Input parameter ,
name VARCHAR(200)  OUT        -- Out parameter declared with the help of OUT keyword
 ) AS BEGIN SELECT name FROM tbl_registration WHERE id=@sid END

 

and  excute procedure

 

Here Input Parameter Means which value is passing for given parameter in store procedure.

when we executed store procedure then asked  for enter value or id.

 

If we enter 2 value then result is sandy.

We know more about exceution of store procedure