MySQL Forums
Forum List  »  Stored Procedures

Re: Please Help Need Search Stored Proc
Posted by: Peter Brawley
Date: March 16, 2009 12:47PM

No, no, no. You cannot delegate query generation to MySQL. It's your job. You need something like ...
BEGIN
  SET @sql = 
  "DEFAULT "SELECT car.id
  , model.manufacturer
  , model.modelname
  , car.year
  , color.color_name
  , interior_color.interior_color
  , car.doors
  , car.retailprice
  , car.dealercost
  , transmission_type.transmission_type
  FROM car
  JOIN model ON car.model_id=model.id 
  JOIN color ON car.color_id=color.id 
  JOIN interior_color ON interior_color.id=car.interior_color_id 
  JOIN transmission_type ON car.transmission_type_id=transmission_type.id
  WHERE 1=1";
  IF mydoors IS NOT NULL THEN
    SET @sql = CONCAT(@sql,' AND car.doors LIKE ',char(39),'%',mydoors,'%',char(39));
  END IF;
  IF mymodel IS NOT NULL THEN
    SET @sql = CONCAT(@sql,' AND car.model LIKE ', char(39),'%',mymodel,'%',char(39));
  END IF;
  -- and so on for each additional param ...
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DROP PREPARE stmt;



Edited 2 time(s). Last edit at 03/17/2009 09:47AM by Peter Brawley.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Please Help Need Search Stored Proc
1976
March 16, 2009 12:47PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.