Stored procedure for advanced search
My Table:
CR_DETAILS
CR_REF_NO VARCHAR(60), ORIGIN_ID INT, DATE_RECEIVED DATETIME, STATUS_ID INT
I want to develop a stored procedure to return advanced search results:
There exists search page with the fields mentioned in the above table. User can fill as many columns he is sure.
For the above table, to write stored procedure, some requirements are also there like below:
There exists a dropdownlist opposite to DATE_RECEIVED
field with (<, >, =) options.
User enters s开发者_JAVA百科ome value against to DATE_RECEIVED
field and he can select on of the operator. Then the search results should satisfy the condition mentioned.
Can anybody please suggest me how can I do this?
As of now, I have written the following stored procedure, but not working :(
ALTER PROCEDURE usp_AdvancedSearchCR
@CR_REF_NO VARCHAR(35),
@ORIGIN_ID INT,
@DATE_RECEIVED DATETIME,
@DATE_RECEIVED_COMPARE_STYLE CHAR,
@STATUS_ID INT
AS
BEGIN
SELECT * FROM CR_DETAILS WHERE
((CR_REF_NO LIKE '%' + @CR_REF_NO + '%') OR (CR_REF_NO IS NULL)) AND
((ORIGIN_ID = @ORIGIN_ID) OR (@ORIGIN_ID = 0)) AND
--I am not understanding how to write code for DATE_RECEIVED
((STATUS_ID = @STATUS_ID) OR (@STATUS_ID = 0))
END
While testing the stored procedure, I have not entered any value for DATE_RECEIVED
and tested, but I am not getting any results. Hence I understood that my other code is also wrong.
Please suggest me, it is urgent for me.
You can write it out in the long form. Replace your comment with the following:
(
(@DATE_RECEIVED_COMPARE_STYLE = '<' AND DATE_RECEIVED < @DATE_RECEIVED) OR
(@DATE_RECEIVED_COMPARE_STYLE = '>' AND DATE_RECEIVED > @DATE_RECEIVED) OR
(@DATE_RECEIVED_COMPARE_STYLE = '=' AND DATE_RECEIVED = @DATE_RECEIVED)
) AND
精彩评论