简体   繁体   中英

Passing table name and column name dynamically to PL/SQL Stored procedure

I am trying to pass table name and column name to a stored procedure in oracle , but it gives me following error: table or view does not exist

Below is the code:

create or replace procedure jz_dynamic_sql_statement
    (p_table_name    in varchar2,
     p_col1_name     in varchar2,
     p_check_result  out integer)

  as

    v_error_cd          est_runtime_error_log.error_cd%type;
    v_error_msg         est_runtime_error_log.error_msg%type;
    v_sql               varchar2(1024);
    v_result            number(10);

  begin
    v_result    := 0;
    v_sql       := 'select  count(*)  from ' || p_table_name ||' WHERE COLUMNNAME=' || p_col1_name;


    execute immediate v_sql into v_result;
    p_check_result := v_result;

  end;

If the error coming back says the table does not exist then that means the table you pass in does not exist or the user that the procedure runs under cannot access it.

You could add a dbms_output.put_line statement to display the query that you are building and then try running it yourself, before you attempt the execute immediate. Then you know what errors you need to fix.

dbms_output.put_line('query : '||v_sql);

Be sure to turn on dbms_output.

Also, from what it looks like you are trying to do, you will need to pass the column name AND column value. Unless the tables you are querying will ALWAYS have the column name "COLUMNNAME".

Try this:

v_sql       := 'select  count(*)  from ' || p_table_name ||' WHERE COLUMNNAME=''' || p_col1_name|| '''';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM