Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to use this is with DBMS_SQL.</p> <p>You create a string that represents your SQL statement. You still use bind variables. It's painful.</p> <p>It goes something like this (I haven't compiled this, but it should be close) :-</p> <pre><code>CREATE OR REPLACE FUNCTION find_country( v_name t1.country%TYPE, v_type t2.type%TYPE) /* Hmm, column called type? */ DECLARE v_SQL varchar2(2000); v_select INTEGER; /* "Pointer" to a DBMS_SQL select statement */ v_execute INTEGER; BEGIN v_SQL := 'select column from table1 t1, table2 t2 || 'where t1.id = t2.id'; IF v_name IS NOT NULL THEN v_SQL := v_SQL || ' AND t1.country = :v_name' END IF; IF v_type IS NOT NULL THEN v_SQL := v_SQL || ' AND t2.type = :v_type'; END IF; /* Setup Cursor */ v_select := dbms_sql.open_cursor; dbms_sql.parse( v_select, v_SQL, DBMS_SQL.native); IF v_name IS NOT NULL THEN dbms_sql.bind_variable( v_select, ':v_name', v_name ); END IF; IF v_type IS NOT NULL THEN dbms_sql.bind_variable( v_select, ':v_type', v_type ); END IF; DBMS_SQL.DEFINE_COLUMN(v_select, 1, v_column); /* This is what we have selected */ /* Return value from EXECUTE is undefined for a SELECT */ v_execute := DBMS_SQL.EXECUTE( v_select ); IF DBMS_SQL.FETCH_ROWS( v_select ) &gt; 0 THEN /* A row was found DBMS_SQL.COLUMN_VALUE( v_select, 1, v_column); /* Tidy Up */ DBMS_SQL.CLOSE_CURSOR(v_select); RETURN v_ID_address; ELSE DBMS_SQL.CLOSE_CURSOR(v_select); /* No row */ RETURN NULL; END IF; EXCEPTION WHEN OTHERS THEN IF DBMS_SQL.IS_open(v_select) THEN DBMS_SQL.CLOSE_CURSOR(v_select); END IF; RAISE; END; </code></pre> <p>This approach is so painful compared to just writing the SQL inline that unless you have heaps of columns sometimes it's just easier writing a couple of different versions using this syntax:</p> <pre><code>FOR r IN (SELECT blah FROM blah WHERE t1 = v_t1) LOOP func( r.blah ); END LOOP; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload