> Stored Procedure and example easily in sql?

Stored Procedure and example easily in sql?

Posted at: 2014-12-18 
This is what an stored procedure usually consists of

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

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

GO

replace with the name you want to give the procedure EX:InsertPerson

replace <@Param1, sysname, @p1> = with a parameter you want the procedure to take such as @firstname NvarChar(50)

replace SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2> with any sql statement such as INSERT,SELECT,etc.

and here is a simple example:

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE [dbo].[InsertPerson]

-- Add the parameters for the stored procedure here

@PersonName NVarChar(50)

AS

BEGIN

SET NOCOUNT ON;

DECLARE @PersonNameID INT

INSERT INTO dbo.Person

(PersonName)

VALUES

(@PersonName)



END

so to run this I would type EXEC dbo.InsertHouse 'Ryan'