Home > Ask the Oracle Experts > Oracle Internals Questions & Answers > Problem with incoming parameter in dynamic SQL statement
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Problem with incoming parameter in dynamic SQL statement

Mike Ault EXPERT RESPONSE FROM: Mike Ault

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 27 January 2006

I'm having a problem with dynamic SQL, specifically getting my incoming parameter into the SQL statement. I am using Oracle 9i and this procedure will be part of a package. I have a .NET front end that will generate the WHERE portion of the statement and pass it in.

Here's the code:

SQL> CREATE OR REPLACE PROCEDURE p_Report_Records(sWhere_in IN VARCHAR2)
  2  IS
  3   CURSOR curGetRecs
  4   IS
  5    SELECT InstCode, InstructCode, ProgTitle, DegCode, CIP,
  6    SUBSTR(CIP,1,2) CIP2,
  7    NVL(Deg_Year1, 0) Deg_Year1,
  8    NVL(Deg_Year2, 0) Deg_Year2,
  9    NVL(Deg_Year3, 0) Deg_Year3,
 10    NVL(Deg_Year4, 0) Deg_Year4,
 11    NVL(Deg_Year5, 0) Deg_Year5,
 12    (Deg_Year1+Deg_Year2+Deg_Year3+Deg_Year4+Deg_Year5) Degree_Total,
 13    (Deg_Year1+Deg_Year2+Deg_Year3+Deg_Year4+Deg_Year5/5)
Degree_5YrAvg,
 14    NVL(Enr_Year1, 0) Enr_Year1,
 15    NVL(Enr_Year2, 0) Enr_Year2,
 16    NVL(Enr_Year3, 0) Enr_Year3,
 17    NVL(Enr_Year4, 0) Enr_Year4,
 18    NVL(Enr_Year5, 0) Enr_Year5,
 19    (Enr_Year1+Enr_Year2+Enr_Year3+Enr_Year4+Enr_Year5) Enroll_Total,
 20    (Enr_Year1+Enr_Year2+Enr_Year3+Enr_Year4+Enr_Year5/5)
Enroll_5YrAvg,
 21    LoPro, InstName, AcadYear
 22    FROM ods.v_5YearProductivity; -- || sWHERE_in;
 23 
 24    v_Prod5 ODS.PRODUCTIVITY_5_YEARS%ROWTYPE;
 25 
 26   BEGIN
 27     OPEN curGetRecs;
 28      FETCH curGetRecs INTO v_Prod5;
 29      WHILE curGetRecs%FOUND
 30       LOOP
 31        INSERT INTO ODS.PRODUCTIVITY_5_YEARS
 32          VALUES v_Prod5;
 33        END LOOP;
 34     CLOSE curGetRecs;
This compiles with the WHERE statement commented out (line 22), but when I include it, I get the following two error messages:
LINE/COL ERROR
-------- --------------------------------------------------
5/3      PL/SQL: SQL Statement ignored
22/32    PL/SQL: ORA-00933: SQL command not properly ended
I realize I'm fairly new to Oracle, but this shouldn't be that difficult! Thanks for any help you can provide.

>
EXPERT RESPONSE
Maybe this example will help:
create or replace function get_count (tab_name varchar2) return number as
cur integer;
sql_text varchar2(200);
rowcount NUMBER;
row_proc integer;
BEGIN
        cur:=dbms_sql.open_cursor;
        sql_text:='select count(*) X from '||tab_name;
DBMS_OUTPUT.PUT_LINE(SQL_TEXT);
        dbms_sql.parse(cur,sql_text,dbms_sql.v7);
        dbms_sql.define_column(cur,1,rowcount);
        row_proc:=dbms_sql.execute(cur);
        dbms_sql.column_value(cur,1,rowcount);
DBMS_OUTPUT.PUT_LINE('ROW_COUNT:'||TO_CHAR(ROWCOUNT));
        return rowcount;
end;
/
You have to send the SQL text to be created as a CURSOR. You can't start with a cursor and then modify the statement.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Oracle Internals
How to find a dropped table?
Removing carriage returns at the end of clob fields
Finding length of numeric datatype field
Reading WSID from back-end
Making a select statement faster
Select * from loc
Difference between 9i and 10g OCP exams
Concatenate values separated with commas
How to free up more space for Oracle?
Error after installation of 9i on Red Hat Linux

Oracle internals
LAST_OPER_TYPE column in v$sga_dynamic_components
Inside the Oracle 11g SQL Performance Advisor, part 1
Inside the Oracle 11g SQL Performance Advisor, part 2
How to read a STATSPACK report
Tuning the Oracle database with initialization parameters
Cannot create services in Oracle 9i
Move tables from system tablespace to users tablespace
Move Oracle home to another partition
Limit on CLOB datatype in Oracle 10g
Logging into Oracle utilities when database is down
Oracle internals Research

Oracle SQL
SQL to count values of a status code
Counting NULL columns
Detail rows for accounts that occur three times
Counting a row's NULL columns
Oracle's free SQL Developer adds database migration tool
Latest transaction if no recent prior transactions
Three ways SQL can count rows by type
SQL to select only certain times within a date range
Oracle SQL to test for numerics
Number of rows in multiple tables
Oracle SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
delimiter  (SearchOracle.com)
extent  (SearchOracle.com)
flexfield  (SearchOracle.com)
responsibility  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts