If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > help with CASE statment under WHERE cluase

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-31-08, 04:36
daimous daimous is offline
Registered User
 
Join Date: Sep 2005
Posts: 72
help with CASE statment under WHERE cluase

hi guys! im not familiar with mysql can you please help me to solve my problem? I have a stored procedure below that basically search my student table and in my ELSE clause my WHERE clause has CASE statement under it but im not sure if it is posible in mysql, im just taking my chances, but when i try to execute it it gives me an error...If it is posible to have a CASE statement under WHERE clause, can you help me guys to solve this problem? basically, what im trying to do in my WHERE clause under ELSE clause is search the student table based from the searchType variable e.g. if searchType is equals to 'First Name' then search the student table where StudFName column containes value equal to searchArg ...Hope you get my point..Thanks in advance!
Code:
DELIMITER $$

DROP PROCEDURE IF EXISTS `dbsample`.`usp_StudentInfo_SEARCH` $$
CREATE PROCEDURE `dbsample`.`usp_StudentInfo_SEARCH` (searchArg VARCHAR(20), searchType VARCHAR(20))
BEGIN

  IF searchType = 'Any' THEN
      SELECT *
      FROM student
      WHERE StudFName LIKE searchArg OR
            StudLName LIKE searchArg OR
            StudMName LIKE searchArg;
  ELSE
      SELECT *
      FROM student
      WHERE
        CASE
            WHEN searchType = 'First Name' THEN StudFName
            WHEN searchType = 'Middle Name' THEN StudMName
            WHEN searchType = 'Last Name' THEN StudLName
        END CASE;
        LIKE searchArg;
  END IF;

END $$

DELIMITER ;
Reply With Quote
  #2 (permalink)  
Old 01-31-08, 11:20
ortho ortho is offline
Registered User
 
Join Date: Nov 2006
Location: Quebec
Posts: 172
maybe :

WHERE
CASE
WHEN searchType = 'First Name' THEN StudFName LIKE searchArg
WHEN searchType = 'Middle Name' THEN StudMName LIKE searchArg
WHEN searchType = 'Last Name' THEN StudLName LIKE searchArg
END CASE;


?
__________________
Less is more.
How long is now?
http://www.lesouterrain.com
Reply With Quote
  #3 (permalink)  
Old 01-31-08, 12:02
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
ortho, no, not like that

daimous, if you're getting an error, perhaps you might let us know what it is
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 01-31-08, 19:30
daimous daimous is offline
Registered User
 
Join Date: Sep 2005
Posts: 72
Ooops! Sorry for that..I forgot..
ERROR:
Quote:
Script line: 4 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE
CASE
WHEN searchType = 'First Name' THEN StudFName
' at line 14
Reply With Quote
  #5 (permalink)  
Old 01-31-08, 19:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
possibly it's barfing on the END CASE

it should be END only

and there should be no semicolon after it
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-31-08, 23:33
daimous daimous is offline
Registered User
 
Join Date: Sep 2005
Posts: 72
Quote:
possibly it's barfing on the END CASE

it should be END only
According to MYSQL Documentation it should be END CASE
and it has semicolon ;...

But when i try your suggestion...Amazing! it works!
Quote:
DELIMITER $$

DROP PROCEDURE IF EXISTS `dbsample`.`usp_StudentInfo_SEARCH` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_StudentInfo_SEARCH`(searchArg VARCHAR(20), searchType VARCHAR(20))
BEGIN

IF searchType = 'Any' THEN
SELECT *
FROM student
WHERE StudFName LIKE searchArg OR
StudLName LIKE searchArg OR
StudMName LIKE searchArg
ORDER BY StudLName;
ELSE
SELECT *
FROM student
WHERE
CASE
WHEN searchType = 'First Name' THEN StudFName
WHEN searchType = 'Middle Name' THEN StudMName
WHEN searchType = 'Last Name' THEN StudLName
END
LIKE searchArg;
END IF;

END $$

DELIMITER ;
Thanks a lot!

Last edited by daimous; 01-31-08 at 23:36.
Reply With Quote
  #7 (permalink)  
Old 01-31-08, 23:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
that's the CASE statement, which is different

your WHERE clause uses a CASE expression



from the manual page you linked to:
Quote:
The syntax of the CASE statement used inside stored routines differs slightly from that of the SQL CASE expression described in Section 10.3, “Control Flow Functions”. The CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of END.
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On