1

I'm writing a procedure on Oracle. I need to use it for a search. The form search have 8 params, each params can contains many values. For exemple you can add two movies title (you can choose to not type whole title). you also can tyoe a year and not a title..

I don't know if I can have multi values for my parameters ?

I want to build only on sql query for it but it is possible ? Because my where clause containt 8 params.. I'm lost with this search !

I have this procedure (doesn't worked)

create or replace procedure listerFilms (
unTitreFilm Film.titre%TYPE DEFAULT NULL,
uneAnneeMin  Film.annee%TYPE DEFAULT NULL,
uneAnneeMax  Film.annee%TYPE DEFAULT NULL,
uneVoFilm    Film.langue%TYPE DEFAULT NULL,
unPaysProd  Pays.pays%TYPE DEFAULT NULL,
unGenreFilm Genre.genre%TYPE DEFAULT NULL,
unNomRea    Equipe_Tournage.nomComplet%TYPE DEFAULT NULL,
unNomActeur Equipe_Tournage.nomComplet%TYPE DEFAULT NULL)
is
titreFilm Film.titre%TYPE;
anneeFilm Film.annee%TYPE;

cursor lignesFilm(leTitreFilm Film.titre%TYPE, laAnneeMin  Film.annee%TYPE, laAnneeMax  Film.annee%TYPE, laVoFilm Film.langue%TYPE, lePaysProd  Pays.pays%TYPE, leGenreFilm Genre.genre%TYPE, leNomRea Equipe_Tournage.nomComplet%TYPE, leNomActeur Equipe_Tournage.nomComplet%TYPE) is
    select distinct
        f.titre, f.annee
    from 
        Film f, Pays p, Genre g, Equipe_Tournage rea, Equipe_Tournage act
    where 
        f.titre like '%'||leTitreFilm||'%' and f.annee >= laAnneeMin and f.annee <= laAnneeMax
        and f.langue like '%'||laVoFilm||'%' and p.pays like '%'||lePaysProd||'%' 
        and g.genre like '%'||leGenreFilm||'%' and rea.nomComplet like '%'||leNomRea||'%'
        and act.nomComplet like '%'||leNomActeur||'%';

begin
open lignesFilm(unTitreFilm,uneAnneeMin, uneAnneeMax, uneVoFilm, unPaysProd, unGenreFilm, unNomRea, unNomActeur);
DBMS_OUTPUT.PUT_LINE('---------------------');
DBMS_OUTPUT.PUT_LINE('-- Liste des Films --');
DBMS_OUTPUT.PUT_LINE('---------------------');

EXCEPTION
WHEN NO_DATA_FOUND THEN
    DBMS_OUTPUT.PUT_LINE('Aucun film ne correpond au(x) critere(s) de recherche');
WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR(-20001,'Exception levée par la procédure');

loop
    fetch lignesFilm into titreFilm, anneeFilm;
    -- fetch retoune la ligne suivante

    EXIT WHEN lignesFilm%NOTFOUND;
    -- quit lorsque cursor fini

    DBMS_OUTPUT.PUT_LINE(titreFilm || ' (' || anneeFilm || ')');
end loop;
close lignesFilm;


end;
/

Please help me

1 Answers1

0

It is possible if you use a bit of PL/SQL magic. Here's an example how you can search for multiple titles (just add other parameters that you need).

Firstly, you need a table type as a parameter to hold multiple titles - that's the t_str_tab type.

Secondly, you use the TABLE function on that table parameter to be able to query it, and you access its values by using the pseudocolumn COLUMN_VALUE.

Thirdly, you use a subquery to check if currently processed row has a title matching any of the titles provided in table parameter.

CREATE TABLE movies (id NUMBER, title VARCHAR2(20), release_year NUMBER);

CREATE TYPE t_str_tab IS TABLE OF VARCHAR2(20);
CREATE TYPE t_num_tab IS TABLE OF NUMBER;

INSERT INTO movies VALUES (1, 'First movie', 2010);
INSERT INTO movies VALUES (2, 'Second movie', 2010);
INSERT INTO movies VALUES (3, 'Third movie', 2010);

COMMIT;

CREATE OR REPLACE PROCEDURE search_proc(p_titles IN t_str_tab, p_years IN t_num_tab DEFAULT NULL) AS
  CURSOR c_movies IS
    SELECT m.id, m.title, m.release_year
      FROM movies m
    WHERE
      ((SELECT COUNT(1) FROM TABLE(p_titles)) = 0
        OR EXISTS (
          SELECT 1
            FROM TABLE(p_titles) -- magic
          WHERE upper(m.title) LIKE '%' || UPPER(column_value) || '%')
      )
      AND
      ((SELECT COUNT(1) FROM TABLE(p_years)) = 0
        OR EXISTS (
          SELECT 1
            FROM TABLE(p_years)
          WHERE m.release_year = column_value)
      )
  ;
BEGIN
  FOR v_movie IN c_movies
  LOOP
    dbms_output.put_line(
      'Id: ' || v_movie.id ||
      ', title: ' || v_movie.title ||
      ', release year: ' || v_movie.release_year);
  END LOOP;
END;
/

BEGIN
  dbms_output.put_line('First search:');
  search_proc(t_str_tab('d'));

  dbms_output.put_line('Second search:');
  search_proc(t_str_tab('st', 'nd'));
END;

Output:

First search:
Id: 2, title: Second movie, release year: 2010
Id: 3, title: Third movie, release year: 2010
Second search:
Id: 1, title: First movie, release year: 2010
Id: 2, title: Second movie, release year: 2010

Edit: changed the code to allow optional parameter.

Przemyslaw Kruglej
  • 8,003
  • 2
  • 26
  • 41
  • Okay, I really thank you for it I understand how it's work ! But I still have a problem, I you have 2 parameters, optional, the LIKE won't work : / – Baptiste Lemarcis Oct 16 '13 at 20:06
  • @BaptisteLemarcis It will work if you take the possibility of empty array under consideration - I've edited the code to show you what I mean. – Przemyslaw Kruglej Oct 16 '13 at 20:20
  • Because if you do not specify the optional parameter, the array, `p_years` in this example, will be empty - and, as you pointed out, no rows would be returned for the `EXISTS` condition, so you have to check if either is true: the array is empty (optional parameter not given) or the value (`release_year` in this case) is in the array. – Przemyslaw Kruglej Oct 16 '13 at 20:30