SQL Server using in keyword pass string array query

前端 未结 3 1085
日久生厌
日久生厌 2021-01-16 20:28

I don\'t think the IN clause can accept bind parameters with multiple values. Oracle can\'t and a couple minutes

and query is

declare @setting varch         


        
3条回答
  •  星月不相逢
    2021-01-16 20:46

    You're misunderstanding how strings are treated in SQL Server. The string you have ('''Sales Entry Grid Cursor'',''Customer Mandatory'',''Column Uom'',''Show Marka'',''Show Discount Amount In Grid'',''Show Discount % In Grid'',''Calculation based on Weight *rate''') is one literal value, not multiple values. IN won't "work" against it because it's looking for a row has that whole string's value for PageConfig_settingsName.

    There are 2 options here. The first is to split your string and compare:

    SELECT pageconfig_action
    FROM [RetailSoft].[dbo].[tbl_pageconfig]
         CROSS APPLY STRING_SPLIT(@setting, ',') SS
    WHERE [PageConfig_settingsName] = SS.[value]
      AND PageConfig_CompanyId = 1;
    

    Note you don't needs the quotes around each utem (unless that really have those quotes in their value)

    If you aren't on SQL Server 2016+ search delimitedsplit8k (If you're on 2008), or delimitedsplit8k_lead if you're on 2012/2014.

    Otherwise, you can use a table-value variable and pass each value separately:

    DECLARE @Setting table (setting varchar(255));
    INSERT INTO @Setting (setting)
    VALUES ('Sales Entry Grid Cursor'),
           ('Customer Mandatory'),
           ('Column Uom'),
           ('Show Marka'),
           ('Show Discount Amount In Grid'),
           ('Show Discount % In Grid'),
           ('Calculation based on Weight *rate');
    
    SELECT P.pageconfig_action
    FROM [RetailSoft].[dbo].[tbl_pageconfig] P
         JOIN @Setting S ON S.setting = P.[PageConfig_settingsName]
    WHERE P.PageConfig_CompanyId = 1;
    

提交回复
热议问题