简体   繁体   中英

Handle multiple parameters in SQL Server

I'm using a stored procedure to search in my database and filtering the results on a parameter and I need to filter on unknown multiple parameters.

How can I write a stored procedure that handles this??

Here is my stored procedure :

CREATE PROCEDURE ComplaintRefListOnDistrict
     @District nvarchar(max) = ''
AS
BEGIN
    SET NOCOUNT ON;

    SELECT  
       ComplaintFullID, CustomerName, Customer_Address, CustomerEmail,
       Date, ContractID, CustomerPhoneNumber,ID, Complaintreference_ID, State
    FROM 
       dbo.ComplaintsSmartObject
    LEFT JOIN 
       dbo.UsersDistricts ON dbo.UsersDistricts.District = dbo.ComplaintsSmartObject.District
    WHERE
       (dbo.UsersDistricts.District = @District)
END
GO

Thank you!

First add a helper function, that will split the string you pass to it into rows.

CREATE FUNCTION SplitString 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS 
    @output TABLE(Data NVARCHAR(MAX)) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 
    BEGIN 
        IF (@end = 0) SET @end = LEN(@string) + 1
        INSERT INTO @output (Data) VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
    END 
    RETURN 
END

And now everything is very easy. Use the SQL IN operator:

CREATE PROCEDURE ComplaintRefListOnDistrict
     @District nvarchar(max) = ''
AS
BEGIN
    SET NOCOUNT ON;

    SELECT  
       ComplaintFullID, CustomerName, Customer_Address, CustomerEmail,
       Date, ContractID, CustomerPhoneNumber,ID, Complaintreference_ID, State
    FROM 
       dbo.ComplaintsSmartObject
    LEFT JOIN 
       dbo.UsersDistricts ON dbo.UsersDistricts.District = dbo.ComplaintsSmartObject.District
    WHERE
       (dbo.UsersDistricts.District IN (SELECT Data FROM dbo.SplitString(@District, ',')))
END

HTH.

CREATE PROCEDURE ComplaintRefListOnDistrict
(
 @District nvarchar(max) = NULL
)
AS
BEGIN

SET NOCOUNT ON;
SELECT 
CSC.ComplaintFullID, CSC.CustomerName, CSC.Customer_Address, CSC.CustomerEmail,
CSC.Date, UD.ContractID, UD.CustomerPhoneNumber,UD.ID, UD.Complaintreference_ID, UD.State
FROM 
  dbo.ComplaintsSmartObject CSC
  LEFT JOIN dbo.UsersDistricts UD
    ON UD.District = CSC.District
WHERE
 (
            UD.District LIKE '%' + @District + '%'
            OR @District IS NULL
            OR @District = ''
            )

    ORDER BY CSC.ComplaintFullID, 
    CSC.CustomerName, 
    CSC.Customer_Address, 
    CSC.CustomerEmail,
CSC.Date, UD.ContractID, UD.CustomerPhoneNumber,UD.ID, UD.Complaintreference_ID, UD.State


END
GO

You can create a WHERE clause where you use the unknow parameter or check if it's null

WHERE (dbo.UsersDistricts.District = @District OR District IS NULL)
AND (SomeOtherColumn = @OtherParameter OR OtherParameter IS NULL)

And so on.

I was telling to use this kind of dynamic SQL.

  CREATE PROCEDURE ComplaintRefListOnDistrict
         @District nvarchar(max) = ''
    AS
    BEGIN
        SET NOCOUNT ON;

     declare @sql nvarchar(max)

    set @sql = 'SELECT  
           ComplaintFullID, CustomerName, Customer_Address, CustomerEmail,
           Date, ContractID, CustomerPhoneNumber,ID, Complaintreference_ID, State
        FROM 
           dbo.ComplaintsSmartObject
        LEFT JOIN 
           dbo.UsersDistricts ON dbo.UsersDistricts.District = dbo.ComplaintsSmartObject.District
        WHERE
           (dbo.UsersDistricts.District in (' +  @District + ') )'

    EXEC sp_executesql @sql
    END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM