繁体   English   中英

在 SQL 语句中构建动态 where 条件

[英]Building dynamic where condition in SQL statement

我想根据存储过程输入构建自定义 Where 条件,如果不为空,那么我将在语句中使用它们,否则我不会使用它们。

if @Vendor_Name is not null
    begin 

    set @where += 'Upper(vendors.VENDOR_NAME) LIKE "%"+ UPPER(@Vendor_Name) +"%"'

    end
    else if @Entity is not null
    begin
    set @where += 'AND headers.ORG_ID = @Entity'
    end
select * from table_name where @where

但我得到这个错误

An expression of non-boolean type specified in a context where a condition is expected, near 'set'.

用这个 :

Declare @Where NVARCHAR(MAX) 

...... Create your Where

DECLARE @Command NVARCHAR(MAX) 
Set @Command = 'Select * From SEM.tblMeasureCatalog AS MC ' ;

If( @Where <> '' )
   Set @Comand = @Command + ' Where ' + @Where

Execute SP_ExecuteSQL  @Command

我测试了这个并且它有效

您不能像在这一行中那样简单地将变量放入普通 SQL 中:

select * from table_name where @where;

您需要使用动态 SQL。 所以你可能有类似的东西:

DECLARE @SQL NVARCHAR(MAX) = 'SELECT * FROM Table_Name WHERE 1 = 1 ';
DECLARE @Params NVARCHAR(MAX) = '';

IF @Vendor_Name IS NOT NULL
    BEGIN
        SET @SQL += ' AND UPPER(vendors.VENDOR_NAME) LIKE ''%'' + UPPER(@VendorNameParam) + ''%''';
    END
ELSE IF @Entity IS NOT NULL
    BEGIN
        SET @SQL += ' AND headers.ORG_ID = @EntityParam';
    END;

EXECUTE SP_EXECUTESQL @SQL, N'@VendorNameParam VARCHAR(50), @EntityParam INT', 
                    @VendorNameParam = @Vendor_Name, @EntityParam = @Entity;

我假设您的实际问题更复杂,并且您为此简化了它,但是如果您的所有谓词都是使用IF .. ELSE IF.. ELSE IF添加的,那么您根本不需要动态 SQL,您可以使用:

IF @Vendor_Name IS NOT NULL
    BEGIN
        SELECT  * 
        FROM    Table_Name
        WHERE   UPPER(vendors.VENDOR_NAME) LIKE '%' + UPPER(@Vendor_Name) + '%';
    END
ELSE IF @Entity IS NOT NULL
    BEGIN
        SELECT  * 
        FROM    Table_Name
        WHERE   headers.ORG_ID = @Entity;
    END

老问题,但想补充一下我刚刚发现使用

where CASE WHEN @id = 0 THEN 1 ELSE id END = CASE WHEN @id = 0 THEN 1 ELSE @id END

这是来自以下步骤:

where id = @id

检查@id 是否为空(或无效)或具有有效值,因此使用大小写替换@id:

where id = 
CASE WHEN @id is null
    THEN 0
    ELSE @id
END

这将最终成为

where id = 0 -- if @id is null, still affected your query
where id = @id -- if @id have valid value

因为我们只想在有有效数据的情况下使用这个“where”子句,然后我们也改变 id

where 
CASE WHEN @id is null
    THEN 0
    ELSE id
END 
= 
CASE WHEN @id is null
    THEN 0
    ELSE @id
END

然后将最终成为

where 0 = 0 -- if @id is null and it doesn't affect your query
where id = @id -- if @id have valid value

不要忘记如果 id 是 varchar,使用 0 作为 '0'

CASE WHEN @id is null
    THEN '0'
    ELSE id
END 

我在我的项目中使用它并且运行良好,即使我在一个查询中使用超过 1 个。 请让我知道这个查询是否在更大的数据上花费时间

无需使用 ad-hoc Qurery ( Execute SP_ExecuteSQL )

检查以下逻辑,您可以使用 N 个动态/不确定参数/条件

    -- flages
declare @chk_vendor bit;
declare @chk_entity bit;

-- setting off
set @chk_entity = 0;
set @chk_vendor = 0;

if @Vendor_Name is not null
        begin 
            set @chk_vendor = 1;
        end
    else if @Entity is not null
        begin
            set @chk_entity = 1;
        end


SELECT * FROM  table_name WHERE 
    (Upper(vendors.VENDOR_NAME) LIKE '%' + UPPER(@Vendor_Name) + '%' OR  @chk_vendor = 0)
AND 
(headers.ORG_ID = @Entity  OR @chk_entity = 0)

我发布了这个答案,因为有很多类似的问题,记住标志只会启用或禁用一个条件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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