Solved

How can I make a conditional where in a function or procedure using MySQL

Posted on 2011-03-03
1
323 Views
Last Modified: 2012-05-11
Hello, I need a function or a store procedure that recieves a parameter install, uninstall or all so it make the right select query.

So far I've got this, and it doesn't work. Help please. Thanks.
BEGIN
	DECLARE vTQ INTEGER(11);

	IF(vAssType = 'all',
		SELECT
			COUNT(*) INTO vTQ
		FROM
			assignment
	    WHERE assignment.AssDate >= vDateFrom
    	AND assignment.AssDate <= vDateTo
	,SELECT
			COUNT(*) INTO vTQ
		FROM
			assignment
	    WHERE assignment.AssDate >= vDateFrom
    	AND assignment.AssDate <= vDateTo
        AND assignment.AssType = vAssType
	)

  RETURN vTQ;
END

Open in new window

0
Comment
Question by:IvanGarcete
1 Comment
 
LVL 3

Accepted Solution

by:
mwiercin earned 500 total points
ID: 35030148
Can you be more specific on how do you want to pass these parameters? Are you looking on two separate flags or a single string parameter?

Hope this will help:
DELIMITER ||
CREATE PROCEDURE test_procedure(_my_param int) 
BEGIN
   IF(_my_param = 1) THEN 
     SELECT "first option" ;
   ELSEIF (_my_param = 2) THEN
     SELECT "second option" ;
    ELSE 
      SELECT "completely different"; 
    END IF; 
END;
||
DELIMITER ; 

Open in new window


And the output:

mysql> call test_procedure(3);
+----------------------+
| completely different |
+----------------------+
| completely different | 
+----------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call test_procedure(2);
+---------------+
| second option |
+---------------+
| second option | 
+---------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> call test_procedure(1);
+--------------+
| first option |
+--------------+
| first option | 
+--------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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