Hey Guys,
I am trying to make a Stored Procedure in SQL Server 2008 R2
That Takes 3 Parameters (AnalyserID,Date_From,Date_To)
Actually, The User Might not send the three parameters
so, When the stored procedure is executed it might have 1 parameter value or 2 or three or not taken any parameters at all.
I designed the stored procedure as Follows
CREATE PROCEDURE [dbo].[SP_T_ANALYSER_RESULTS_GET_BY_AnalyserID_Date]
@p_AnalyserID INT,
@p_Date_From Datetime,
@p_Date_To Datetime
AS
DECLARE @query varchar(2000);
SET @query = 'SELECT * FROM T_ANALYSER_RESULTS WHERE T_ANALYSER_RESULTS.AnalysersID = ' + @p_AnalyserID
IF @p_Date_From is not null THEN
@query = @query + ' And T_ANALYSER_RESULTS.ResultDate >' + @p_Date_From
END IF;
IF @p_Date_To is not null THEN
@query = @query + ' T_ANALYSER_RESULTS.ResultDate < ' + @p_Date_To
END IF;
EXEC(@query)
But I am getting Syntax Error
Msg 156, Level 15, State 1, Procedure SP_T_ANALYSER_RESULTS_GET_BY_AnalyserID_Date, Line 8
Incorrect syntax near the keyword 'THEN'.
Msg 156, Level 15, State 1, Procedure SP_T_ANALYSER_RESULTS_GET_BY_AnalyserID_Date, Line 11
Incorrect syntax near the keyword 'THEN'.
Any Ideas PlZ????
Its Urgent!!