将比较运算符作为参数传递并直接在SQL存储过程中使用

时间:2017-01-24 05:29:14

标签: sql sql-server stored-procedures parameters

我有一个SQL存储过程,它需要几个参数。在那里,我需要将条件作为=<>传递,并在查询中使用它们。

目前我正在做的是检查条件是什么,并根据它更改查询的一部分。我想将条件作为参数传递,并直接在查询中使用它。

这就是我现在正在做的事情,

我传递了这些参数:

@software_type nvarchar(50),
@software_id nvarchar(50),
@condition char,
@version_id float

查询:

DECLARE
    @BaseQuery nvarchar(max) = N'SELECT DISTINCT Installed_Software.vm_name FROM Installed_Software INNER JOIN Software ON Installed_Software.software_id = Software.software_id INNER JOIN Software_Version ON Installed_Software.software_id = Software_Version.software_id AND Installed_Software.version_id = Software_Version.version_id AND Software.software_id = Software_Version.software_id',
    @WhereClause nvarchar(max) = ' WHERE 1=1',
    @ParamList nvarchar(max) = N'@sfType nvarchar(50),@sfId nvarchar(50),@verId float,@cond char'

IF @software_type IS NOT NULL
BEGIN
    SET @WhereClause = @WhereClause + ' AND Software.software_type = @sfType';
END

IF @software_id IS NOT NULL
BEGIN 
    SET @WhereClause = @WhereClause + ' AND Installed_Software.software_id=@sfId';
END

IF @version_id IS NOT NULL AND @condition IS NOT NULL
BEGIN
    IF @condition = '='
    BEGIN
        SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id = @verId';
    END
    ELSE IF @condition ='>'
    BEGIN
        SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id > @verId';
    END
    ELSE IF @condition='<'
    BEGIN
        SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id < @verId';
    END
    ELSE IF @condition = '>='
    BEGIN
        SET @WhereClause=@WhereClause + ' AND Installed_Software.version_id >= @verId';
    END
    ELSE IF @condition='<='
    BEGIN
        SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id <= @verId';
    END
    ELSE
    BEGIN
        SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id = @verId';
    END
END

SET @BaseQuery = @BaseQuery + @WhereClause;

EXECUTE sp_executesql @BaseQuery, @ParamList, @sfType=@software_type,  @sfId=@software_id, @verId=@version_id;

我想做的是这样的事情:

IF @version_id IS NOT NULL AND @condition IS NOT NULL
BEGIN       
    SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id' + '@condition' + '@version_id';  
END

这可能吗?

2 个答案:

答案 0 :(得分:1)

我建议不要使用动态sql 我相信以下查询应该返回与动态sql版本相同的结果:

SELECT DISTINCT Installed_Software.vm_name 
FROM Installed_Software 
INNER JOIN Software 
    ON Installed_Software.software_id = Software.software_id 
INNER JOIN Software_Version 
    ON Installed_Software.software_id = Software_Version.software_id 
    AND Installed_Software.version_id = Software_Version.version_id 
    AND Software.software_id = Software_Version.software_id
WHERE
(
    @software_type IS NULL 
    OR Software.software_type = @software_type
)
AND 
(
    @software_id IS NULL 
    OR Installed_Software.software_id = @software_id
)
AND 
(
    @version_id IS NULL 
    OR @condition IS NULL
    OR (@condition = '=' AND Installed_Software.version_id = @version_id)
    OR (@condition = '>' AND Installed_Software.version_id > @version_id)
    OR (@condition = '<' AND Installed_Software.version_id < @version_id)
    OR (@condition = '>=' AND Installed_Software.version_id >= @version_id)
    OR (@condition = '<=' AND Installed_Software.version_id <= @version_id)
)

答案 1 :(得分:0)

这可以吗?

只需在条件之间添加单个空格即可避免语法错误

SET @WhereClause = @WhereClause + ' AND Installed_Software.version_id ' + '@condition' + ' @version_id';
                                                                 ----^              -----^