Search code examples
mysqlprocedure

MySQL Procedure unable to perform as expected


CREATE PROCEDURE `userCredentialsWeight`(IN data1 varchar(50), IN data2 varchar(250))
BEGIN

    /* all declaration */

    IF (SELECT field1 FROM user_credentials WHERE field1 = data1 LIMIT 1 )
    THEN
        SET DATA_WEIGHT = 75;
    ELSE
        SET DATA_WEIGHT = 100;
    END IF;

    /* More calculations here */  ---> this block contains all the calculation,
 actually i am new to procedure, i have done this using 
direct query from application but it requires 4 database requests 
which I want to minimize here

END

Now my issue is the condition after WHERE is not getting a value of data1.

Platform - Mysql (i am using MYSQL Workbench)


Solution

  • Test

    SELECT CASE WHEN COUNT(*) = 0 
                THEN 100
                ELSE 75
                END
    INTO data_weight 
    FROM user_credentials
    WHERE field1 = data1;