Wednesday, August 20, 2008

Stored Procedure

Syntax to create Stored Procedure.

CREATE PROCEDURE
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> = ,
<@Param2, sysname, @p2> =
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END


Example :
Suppose there is a table StudentMast Having three fields Roll,Name,Address. I want to make an SP(i.e stored procedure) which will take one parameter and finds records which have name = the parameter.


CREATE PROCEDURE Find_Student_By_Name
@Name Varchar(50) ---This is a parameter
AS

BEGIN
Select *
From StudentMast
Where Name=@Name
END

No comments: