Solved

Mysql - Assign variable on EXISTS clause.

Posted on 2009-07-04
2
221 Views
Last Modified: 2012-05-07
How to assign a value inside a EXISTS clause in a Mysql function.

This code bellow (UID) is returning NULL.
Please help.
CREATE FUNCTION LOG_STATUS (LID int) RETURNS char(9)

BEGIN

    DECLARE UID char(9);

    IF EXISTS(SELECT USR_ID INTO UID FROM `logs` WHERE ID=LID AND LOGOUT='0000-00-00 00:00:00') THEN

        RETURN UID;

    ELSE

       RETURN 'NO';

    END IF;

END

Open in new window

0
Comment
Question by:ricarmpo
2 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24777821
What is UID for that row? It could be actually NULL.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24777825
you cannot put INTO inside of EXISTS.

I would consider this method:
CREATE FUNCTION LOG_STATUS (LID int) RETURNS char(9)
BEGIN
    DECLARE UID char(9);
    SELECT USR_ID INTO UID FROM `logs` WHERE ID=LID AND LOGOUT='0000-00-00 00:00:00'; 
    RETURN COALESCE(UID, 'NO');
END

Open in new window

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now