1

When executing an SQL statement in PLSQL with

DBMS_SQL.EXECUTE('insert into tablename VALUES(aNumber)');

It gives an ASCII character error. And when inserting it directly it succeeds and inserts it.

4
  • 2
    Please provide a complete example. As it is tablevalue is an invalid identifier and can't be inserted. Commented May 30, 2013 at 13:50
  • 3
    The closing parenthesis is missing. Commented May 30, 2013 at 14:30
  • 1
    Why do you want to use DBMS_SQL? If you really need dynamic SQL, EXECUTE IMMEDIATE seems a much more sensible choice for such a simple task. Commented May 30, 2013 at 14:40
  • @Toru That was just a typo when I wrote this question. Commented May 31, 2013 at 10:49

1 Answer 1

7

In PL/SQL you could write the INSERT statement directly.

DECLARE
  tablevalue      varchar2(200);
BEGIN
  tablevalue := 'Hello World!';

  INSERT   INTO tablename
         VALUES (tablevalue);
END;

Your statement fails because that is not the way DBMS_SQL.EXECUTE works. Check out the documentation and the example: http://docs.oracle.com/cd/B28359_01/appdev.111/b28419/d_sql.htm#BABBFFFJ

According to the example given in the reference documentation you should do it like this (first you prepare the statement, then bind the variable and then run it).

CREATE OR REPLACE PROCEDURE demo(tablevalue IN varchar2) AS
    cursor_name INTEGER;
    rows_processed INTEGER;
BEGIN
    cursor_name := dbms_sql.open_cursor;
    DBMS_SQL.PARSE(cursor_name, 'INSERT INTO tablename VALUES(:x)',
                   DBMS_SQL.NATIVE);
    DBMS_SQL.BIND_VARIABLE(cursor_name, ':x', tablevalue);
    rows_processed := DBMS_SQL.EXECUTE(cursor_name);
    DBMS_SQL.CLOSE_CURSOR(cursor_name);
EXCEPTION
WHEN OTHERS THEN
    DBMS_SQL.CLOSE_CURSOR(cursor_name);
    raise;
END;

You use it then like this

 exec demo('something');

Hope it helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.