简体   繁体   English

如何在存储过程查询中使用IF条件

[英]How to use IF condition in Stored Procedure Query

Here is my Stored procedure Query. 这是我的存储过程查询。 Here if typee is existing it should add the 2nd condition or it should not add. 如果typee存在,则应在其中添加第二个条件,也可以不添加。 My table structure is like this 我的表结构是这样的

|id | name | typee |is_primary|
|1  |IND   |0      |1         |
|2  |SRIL  |1      |0         |

My Stored Procedure query is following 我的存储过程查询如下

CREATE DEFINER=`root`@`localhost` PROCEDURE `testIfCondition`(IN countryId int(11),IN typee INT(11),IN is_primary INT(11),
out name VARCHAR(50))
BEGIN
     select name from country where id = id
        if typee != 0 then
          and typee= typee 
        end if;
       if is_primary !=0 then
          and primary = primary 
        end if;
END

In the above query which ever is having values has to get execute. 在上面的查询中,曾经有值的查询必须执行。 If anyone know please help me out in this. 如果有人知道,请帮助我。 When i am trying it is giving some syntax error. 当我尝试它给一些语法错误。 Please tell me correct syntax to solve my issue. 请告诉我正确的语法来解决我的问题。

Thanks in advance !!!! 提前致谢 !!!!

I don't think that you could add more condition using the IF statement. 我认为您不能使用IF语句添加更多条件。 You can probably select it twice or more and add it to a temporary table , for example like this: 您可能会选择两次或更多次并将其添加到temporary table ,例如:

First create the temporary table 首先创建temporary table

DECLARE @tempTable TABLE (
    ID INT,
    Name NVARCHAR(250),
    Typee INT
)

Then insert the first SELECT result to the @tempTable 然后将第一个SELECT结果插入@tempTable

INSERT INTO @tempTable
SELECT * FROM country WHERE id = countryId

Then use the condition to check if the typee is NULL or not 然后使用条件来检查,如果typeeNULL或不

IF typee IS NOT NULL AND is_primary IS NOT NULL THEN
    SELECT * FROM @tempTable WHERE typee = typee AND is_primary = is_primary
ELSE IF typee IS NOT NULL THEN
    SELECT * FROM @tempTable WHERE typee = typee
ELSE IF is_primary IS NOT NULL THEN
    SELECT * FROM @tempTable WHERE is_primary = is_primary
ELSE
    SELECT * FROM @tempTable
END

though I think this is not the best practice to do it. 尽管我认为这不是最佳做法。

EDIT: 编辑:

Based on the other answer here , you could create the query into a string first, then execute it. 根据此处的另一个答案,您可以先将query创建为字符串,然后执行query Your sp should look like this: 您的sp应该如下所示:

CREATE DEFINER=`root`@`localhost` PROCEDURE `testIfCondition`(IN countryId int(11),IN typee INT(11),IN is_primary INT(11),
out name VARCHAR(50))
BEGIN
    SET @query = CONCAT('select name from country where id = ', countryId);
    if typee <> 0 then
        SET @query = CONCAT(@query, ' AND typee = ', typee);
    end if;
    if is_primary <> 0 then
        SET @query = CONCAT(@query, ' AND is_primary = ', is_primary);
    end if;

    PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END

You can look here to see the execute query from the string. 您可以在此处查看从字符串执行查询。

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

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