skip to Main Content

I’m running in lack of understanding why a field value (varchar) containing TEXT of a table cannot be evaluated in a stored procedure.

set @Language=(Select Language from tblcustomer where cid=123);
If @Language='english' then
    set @salutation='Ladies and gentlemen,';
else
    set @salutation='Sehr geehrte Damen und Herren!';
end if;

The result is always the second option.

If I test it the select shows NULL for @Language

SELECT   @Language, Language from tblcustomer where cid=123;
result=  NULL, english

Same when I

DECLARE xxLanguage varchar(20);
set xxLanguage=(Select Language from tblcustomer where cid=123);

 SELECT xxLanguage, Language from tblcustomer where cid=123;
       NULL, english

But doing this

set @Language=(SELECT IFNULL((SELECT 1 from tblcustomer where cid=123 and Language ='english'),0);

gives the correct value 1. But then I cannot continue with the correct if..end result again.

If @Language=1 then
        set @salutation='Ladies and gentlemen,';
    else
        set @salutation='Sehr geehrte Damen und Herren!';
    end if;

gives the second (incorrect) option (‘Sehr geehrte Damen und Herren!’).

Any idea what’s going wrong here?
Thanks

2

Answers


  1. Chosen as BEST ANSWER

    I found the error. A very stupid one! cid value comes from a IN parameter and was NULL iso 123!!

    Therefor @Language was NULL for all the time!!!

    set @Language=(Select Language from tblcustomer where cid=123);
    

    Sorry about that!


  2. Try
    set @salutation=If (@Language='english', 'Ladies and gentlemen,', 'Sehr geehrte Damen und Herren!');
    https://dbfiddle.uk/nY6vjYLP

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search