Solved

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

Posted on 2011-03-03
1
324 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
[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
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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 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