Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now