Thursday, October 21, 2010

How to write Stored Procedure in SQL SERVER

I will try to write store procedure in sql.It is for begineers purpose.

To begin write away to the procedure you will 1st write CREATE PROCEDURE <PROCEDURE_NAME>

--Here i will try to display simple example of Parametric procedure (SQL SERVER 2005)


SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE procedure_first
-- Add the parameters for the stored procedure here
(
@firstname as varchar(max),
@lastname as varchar(max)
)
with recompile
AS
DECLARE
@strsql nvarchar(max)
BEGIN
SET NOCOUNT ON;

set @strsql = N'select * from ex '
set @strsql = @strsql + "where firstname = '" +@firstname+ "' and lastname = '" +@lastname+ "'"

       print @strsql

exec sp_executeSql @strSql
END
GO

--------------------------------------------------

How to Execute?
Fire Following Command.

EXEC dbo.procedure_first 'Sagar','Awale'

Output :

and Message also displayed (plz check next image..)

No comments: