?
Solved

Mysql - Assign variable on EXISTS clause.

Posted on 2009-07-04
2
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

765 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