Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql - Assign variable on EXISTS clause.

Posted on 2009-07-04
2
Medium Priority
?
242 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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 Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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