Wednesday, March 21, 2012

hi i want to know how to write stored procedure ..then i have to include (IF condition ) w

hi i want to know how to write stored procedure ..then i have to include (IF condition ) with SP ..

let me this post ..............anybody ????

CREATEPROCEDURE name_of_procedure
-- Add the parameters for the stored procedure here
@.passedparam1 int,
@.passedparam2 smalldatetime

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

SET NOCOUNTON;

-- Insert statements for procedure here

SELECT [COLOFINTS], [OTHERCOL] FROM MYDBNAME WHERE ([COLOFINTS] = @.passedparam1 AND [DATEENTERED] > @.passedparam2

END

|||

CREATEPROCEDURE name_of_procedure
-- Add the parameters for the stored procedure here
@.passedparam1 int,
@.passedparam2 smalldatetime

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.

SET NOCOUNTON;

-- Insert statements for procedure here

IF @.passedparam2 < GETDATE()

SELECT [COLOFINTS], [OTHERCOL]FROM MYDBNAMEWHERE ([COLOFINTS] = @.passedparam1 AND [DATEENTERED] > @.passedparam2)

ELSE

SELECT [COLOFINTS], [OTHERCOL]FROM MYDBNAMEWHERE [COLOFINTS] = @.passedparam1

END

|||

Just to clarify something that clevesteve wrote, if the IF has > 1 statement, you must wrap it in BEGIN END, for example,

IF @.passedparam2 < GETDATE()
BEGIN
SELECT [COLOFINTS], [OTHERCOL] FROM MYDBNAME WHERE ([COLOFINTS] = @.passedparam1 AND [DATEENTERED] > @.passedparam2)
...some other statement
END
ELSE
BEGIN
SELECT [COLOFINTS], [OTHERCOL] FROM MYDBNAME WHERE [COLOFINTS] = @.passedparam1
..some other statemetn
END

|||

hi thaka for reply

please send me a any tutorial of these stored procudre ...bcz am a beginner of these

|||

You can find many such on the web but here's a start on using T-SQL http://www.sql-server-performance.com/articles/dba/stored_procedures_basics_p1.aspx

For more, just google "sql server how to write stored procedures"

Sql server 2005 also supports writing procs in .net but I have no experience myself with this

No comments:

Post a Comment