我想将一个类似参数设置为一个变量并允许该变量接受通配符(mssql 2005)。如果我使用我的参数集'%'它只返回一个子集,但如果我硬编码查询使用'%'它将返回完整的集合。为什么变量的行为可能与字符串不同?声明变量的行为与硬编码字符串不同
DECLARE @wareno char(4);
SET @wareno = '%';
select @wareno as a, * from waredesc where wareno like @wareno;
VS
DECLARE @wareno char(4);
SET @wareno = '%';
select @wareno as a, * from waredesc where wareno like '%';
完整场景基于标志切换但高于
DECLARE @wareno char(4);
DECLARE @delprods bit;
/**
SET THESE PARAMETERS
**/
SET @wareno = '1';
SET @delprods = 'true'; /** if true all the warehouses should also be emptied for safety - products are held at a company level!**/
IF @delprods = 1
BEGIN
SET @wareno = '%';
END
select @wareno as a, * from waredesc where wareno like @wareno;
由于
@wareno等于'%000' –
但是你想要做什么?你没有指定使用'LIKE'谓词比较哪些字符串,就像'%''为什么? –
我实际上是在预先设定的值和基于另一个标志的通配符之间切换@wareno。在某些情况下,我需要覆盖输入的值并选择所有wareno – Joe