不要认为IN子句可以接受具有多个值的绑定参数。甲骨文不能和几分钟 而查询是
declare @setting varchar(max)
set @setting ='''Sales Entry Grid Cursor'',''Customer Mandatory'',''Coloum Uom'',''Show Marka'',''Show Discount Amount In Grid'',''Show Discount % In Grid'',''Calculation based on Weight *rate'''
存储过程为
Select pageconfig_action from [RetailSoft].[dbo].[tbl_pageconfig]
Where [PageConfig_settingsName] in(@setting)
and PageConfig_CompanyId=1
结果为空
并直接在关键字
中传递字符串Select pageconfig_action from [RetailSoft].[dbo].[tbl_pageconfig]
Where [PageConfig_settingsName] in('Sales Entry Grid Cursor','Customer Mandatory','Coloum Uom','Show Marka','Show Discount Amount In Grid','Show Discount % In Grid','Calculation based on Weight *rate')
and PageConfig_CompanyId=1
然后结果就可以了
答案 0 :(得分:0)
您需要将Setting
设为表格,而不是varchar。
那么就不需要动态sql了,您可以像这样保持简单
declare @Setting table (name varchar(50))
insert into @Setting (name)
values ('Sales Entry Grid Cursor'),
('Customer Mandatory'),
('Coloum Uom'),
('Show Marka'),
('Show Discount Amount In Grid'),
('Show Discount % In Grid'),
('Calculation based on Weight *rate')
Select pageconfig_action
from [RetailSoft].[dbo].[tbl_pageconfig]
Where [PageConfig_settingsName] in (select name from @setting)
and PageConfig_CompanyId=1
答案 1 :(得分:0)
在SQL SERVER 2016+中:
使用内置的string_split函数。请注意,不再需要多余的单引号:
DECLARE @setting varchar(max)
set @setting ='Sales Entry Grid Cursor,Customer Mandatory,Coloum Uom,Show Marka,Show Discount Amount In Grid,Show Discount % In Grid,Calculation based on Weight *rate'
Select pageconfig_action from [RetailSoft].[dbo].[tbl_pageconfig]
Where [PageConfig_settingsName] in(SELECT value FROM string_split(@setting, ',') )
and PageConfig_CompanyId=1
如果您运行的SQL Server早于SQL 2016,则@GuidoG的答案是一种更可取的方法
答案 2 :(得分:0)
您误解了SQL Server中如何处理字符串。您拥有的字符串('''Sales Entry Grid Cursor'',''Customer Mandatory'',''Coloum Uom'',''Show Marka'',''Show Discount Amount In Grid'',''Show Discount % In Grid'',''Calculation based on Weight *rate'''
是一个文字值,而不是多个值。 IN
不会针对它“工作”,因为它正在寻找一行包含PageConfig_settingsName
的整个字符串值的行。
这里有2个选项。首先是分割您的字符串并进行比较:
SELECT pageconfig_action
FROM [RetailSoft].[dbo].[tbl_pageconfig]
CROSS APPLY STRING_SPLIT(@setting, ',') SS
WHERE [PageConfig_settingsName] = SS.[value]
AND PageConfig_CompanyId = 1;
请注意,您不需要每个utem周围的引号(除非它们的值确实包含这些引号)
如果您不在SQL Server 2016+上,请搜索delimitedsplit8k(如果您使用的是2008),或者如果您使用的是2012/2014,则请使用delimitedsplit8k_lead。
否则,您可以使用表值变量并分别传递每个值:
DECLARE @Setting table (setting varchar(255));
INSERT INTO @Setting (setting)
VALUES ('Sales Entry Grid Cursor'),
('Customer Mandatory'),
('Coloum 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;