简体   繁体   中英

How can I pass a string type value into a Stored Procedure?

I am trying to call this procedure with the following parameters:

proc2_del_rows('EMP', 'JOB = CLERK')

But this results in an error. Why?

CREATE OR REPLACE PROCEDURE proc2_del_rows
        (v_tname        VARCHAR2,
         v_condition    VARCHAR2 DEFAULT NULL)
AS
        sql_stmt        VARCHAR2(500);
        where_clause    VARCHAR2(200) := 'WHERE'||' '||v_condition;
BEGIN
        IF v_condition IS NULL THEN
            where_clause := NULL;
        END IF;

        sql_stmt := 'DELETE FROM :1'||' '||where_clause;

        EXECUTE IMMEDIATE sql_stmt USING v_tname;
        COMMIT;
END;

You are calling it with the condition set to 'JOB = CLERK'. This won't work, as CLERK needs to have quotes around it. To embed quotes in a quote-delimited string, double them up: 'JOB = ''CLERK'''

proc2_del_rows('EMP', 'JOB = ''CLERK''')

The error in the first comment below is due to incorrect syntax for the table in the query. Try:

sql_stmt := 'DELETE FROM '|| v_tname ||' '||where_clause;

 EXECUTE IMMEDIATE sql_stmt;

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