Greetings experts,
I pray that all is well and safe.
I have been stuck trying to resolve an error on the stored procedure below.
A little Background: When users attempt to look up cases, they can search by Casenumber, defendant Name or Plaintiff name.
These three are radio button options.
There is a search box where a user can enter a value.
If you user checks either the defendant name or plaintiff name, then user enters name value in the search box.
If user checks the casenumber box, the user must enter casenumber value in the search box in other to get results if record exists.
Finally, there is the case type. Case can be civil, criminal or both.
So, user checks a box for either defendant name, plaintiff name, or casenumber, enters search value in the search box and selects case type of civil, criminal,or both from dropdown.
The problem we have been having now for quite sometime is that regardless of whether user is search by civil, criminal or both case type, as long as user checks the plaintiff radio box, the page blows up with an error message: Incorrect syntax near 'WHERE'
Can someone please point me in the right direction of what I am doing wrong with the stored proc below?
ALTER PROCEDURE [dbo].[retrieveCases] @searchValue VARCHAR(max),
@CaseType VARCHAR(1),
@DFName BIT,
@CaseNo BIT,
@PLName BIT
AS
BEGIN
DECLARE @STR VARCHAR(max)
-- BEGIN CIVIL CASE --
IF @CaseType = '1'
BEGIN
SET @STR='SELECT DISTINCT CaseId, [TypeId] , [CASENO] , [Name] , [Status] , [Type], [DISPDATE] , CaseType FROM [dbo].[SearchCatalog1] WHERE '
IF @CaseNo <> '0'
BEGIN
SET @STR +=' CONTAINS([CASENO], ''"*'
+ @searchValue + '*"'') '
END
IF @DFName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0'
BEGIN
SET @STR +=' OR '
END
------------------------------------------------
SET @STR +=' (CONTAINS([Name], ''"*'
+ @searchValue + '*"'') AND CONTAINS([Type],''DEFENDANT'')) '
END
IF @PLName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0' or @DFName <> '0'
BEGIN
SET @STR +=' OR '
END
-------------------------------------------------
SET @STR +=' (CONTAINS([Name], ''"*'
+ @searchValue + '*"'') AND CONTAINS([Type],''PLAINTIFF'' ))'
END
END
-- END CIVIL CASE --
-- BEGIN CRIMINAL CASE --
ELSE IF @CaseType = '2'
BEGIN
SET @STR='SELECT DISTINCT CaseId, [TypeId] , [CASENO] , [Name] , [Status] , [Type], [DISPDATE] , CaseType FROM [dbo].[SearchCatalog2] WHERE '
IF @CaseNo <> '0'
BEGIN
SET @STR +='CONTAINS([CASENO], ''"*'
+ @searchValue + '*"'') '
END
IF @DFName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0'
BEGIN
SET @STR +=' OR '
END
------------------------------------------------
SET @STR +='CONTAINS([NAME], ''"*'
+ @searchValue + '*"'') '
END
END
-- END CRIMINAL CASE --
-- BEGIN BOTH CASES --
ELSE
BEGIN
SET @STR='SELECT DISTINCT CaseId, [TypeId] , [CASENO] , [Name] , [Status] , [Type], [DISPDATE] , CaseType FROM [dbo].[SearchCatalog1] WHERE '
IF @CaseNo <> '0'
BEGIN
SET @STR +='CONTAINS([CASENO], ''"*'
+ @searchValue + '*"'') '
END
IF @DFName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0'
BEGIN
SET @STR +=' OR '
END
------------------------------------------------
SET @STR +=' (CONTAINS([Name], ''"*'
+ @searchValue + '*"'') AND CONTAINS([Type],''DEFENDANT'')) '
END
IF @PLName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0' or @DFName <> '0'
BEGIN
SET @STR +=' OR '
END
-------------------------------------------------
SET @STR +=' (CONTAINS([Name], ''"*'
+ @searchValue + '*"'') AND CONTAINS([Type],''PLAINTIFF'' ))'
END
SET @STR +=' UNION ALL '
SET @STR +=' SELECT DISTINCT CaseId, [TypeId] , [CASENO] , [Name] , [Status] , [Type], [DISPDATE] , CaseType FROM [dbo].[SearchCatalog2] WHERE '
IF @CaseNo <> '0'
BEGIN
SET @STR +='CONTAINS([CASENO], ''"*'
+ @searchValue + '*"'') '
END
IF @DFName <> '0'
BEGIN
-- Set OR in case that previous condition exists
IF @CaseNo <> '0'
BEGIN
SET @STR +=' OR '
END
------------------------------------------------
SET @STR +='CONTAINS([NAME], ''"*'
+ @searchValue + '*"'') '
END
END
print @Str
EXEC (@STR)
END