Solved

Mysql - Assign variable on EXISTS clause.

Posted on 2009-07-04
2
232 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 143

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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