我正在尝试将类似参数设置为变量并允许该变量接受通配符(mssql 2005)。如果我将参数集设置为“%”,它只返回部分子集,但如果我将查询硬编码为使用“%”,它会返回完整集。为什么变量的行为可能与字符串不同?
DECLARE @wareno char(4);
SET @wareno = '%';
select @wareno as a, * from waredesc where wareno like @wareno;
对比
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;
谢谢