V
V
Vladimir Golub2021-07-26 15:12:59
MySQL
Vladimir Golub, 2021-07-26 15:12:59

How to fulfill different conditions depending on the input parameter?

I want to execute different where depending on the input parameter?

CREATE PROCEDURE proc_IF (IN param1 INT)  
BEGIN  
    SELECT * FROM articles 
  
    IF param1 = 0 THEN  
        WHERE name = 'Тест'
    ELSE  
        WHERE name = 'Проверка'  
    END IF;  
END

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-07-26
@RazerVG

CREATE PROCEDURE proc_IF (IN param1 INT)  
BEGIN  
    SET @query = CONCAT('SELECT * FROM articles ', IF(param1 = 0, "  WHERE NAME = 'Тест';", , "  WHERE NAME = 'Проверка';");
    
    PREPARE stmt FROM @query;
    
    EXECUTE stmt;
     
    DEALLOCATE PREPARE stmt;
END

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question