Solved

Mysql - Assign variable on EXISTS clause.

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 58
Sql case statement to calculate totals 5 52
When are cursors useful? 8 55
What does "Between" mean? 6 35
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.

738 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