Error at Line Number 25: I am writing stored procedure which should return data based on the combination selected by the user. For Example I have 3 different type of filter(City/Area/VendorType). If User selects City=xyz and Area=pqr then result should be all data with City=xyz and Area=pqr only. If None of the option is selected by the user then I am setting value for all 3 parameter is equals to null AND should return all row from the database. Note : Above 3 column should always have data in database. I have created one [WEBMethod] inside which I am calling this stored procedure using sql connection. And passing all parameters using AJAX jquery.
ALTER PROCEDURE [dbo].spGetVendorbyFilter
@PageNumber INT,
@PageSize INT,
@city VARCHAR(200),
@area VARCHAR(200),
@vendortype VARCHAR(200)
AS
BEGIN
DECLARE @StartRow INT
DECLARE @EndRow INT
SET @StartRow = ( ( @PageNumber - 1 ) * @PageSize ) + 1;
SET @EndRow= @PageNumber * @PageSize;
WITH Result
AS (
SELECT *,
Row_number()
OVER (
ORDER BY VendorID ASC) RowNumber
FROM tblVendor
)
IF (@city IS NOT NULL AND @area IS NULL AND @vendortype IS NULL)
SELECT *
FROM Result where City=@city AND RowNumber BETWEEN @StartRow and @EndRow
ELSE IF (@city IS NULL AND @area IS NOT NULL AND @vendortype IS NULL)
SELECT *
FROM Result where Area=@area AND RowNumber BETWEEN @StartRow and @EndRow
ELSE IF (@city IS NULL AND @area IS NULL AND @vendortype IS NOT NULL)
SELECT *
FROM Result where Category=@vendortype AND RowNumber BETWEEN @StartRow and @EndRow
ELSE IF (@city IS NOT NULL AND @area IS NOT NULL AND @vendortype IS NULL)
SELECT *
FROM Result where City=@city And Area=@area AND RowNumber BETWEEN @StartRow and @EndRow
ELSE IF (@city IS NOT NULL AND @area IS NULL AND @vendortype IS NOT NULL)
SELECT *
FROM Result where City=@city And Category=@vendortype AND RowNumber BETWEEN @StartRow and @EndRow
ELSE IF (@city IS NULL AND @area IS NOT NULL AND @vendortype IS NOT NULL)
SELECT *
FROM Result where Area=@area And Category=@vendortype AND RowNumber BETWEEN @StartRow and @EndRow
ELSE
SELECT *
FROM Result WHERE RowNumber BETWEEN @StartRow and @EndRow
END