0

I encountered the following error, i'm learning about DBMS_SQL. I was playing around with the below code which drops a table and recreates a new table. I noticed for the recreation of the new table, i'm running into a character constraint. Just wondering what the best solution is to fix this.

I've tried to concatenating two strings but that didnt work. 'CREATE TABLE students_12345(s_id NUMBER, fname VARCHAR2(30),lname VARCHAR2(30),tname VARCHAR2(100), score NUMBER, exam_result VARCHAR2(6))';

Its probably something really simple but its taken me a couple of hours to get to this point in the code and my eyes are going square shaped at this point.

Thanks in advance!

Error starting at line : 48 in command - EXECUTE RecreateTempTable('a') Error report - ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at "SYS.RECREATETEMPTABLE", line 33 ORA-06512: at line 1 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.

CREATE TABLE students_12345
  (student_id   NUMBER,  
   first_name   VARCHAR2(30),
   last_name    VARCHAR2(30),
   test_name    VARCHAR2(100),
   score        NUMBER,
   exam_result  VARCHAR2(6));

/
-----------------------------------------------------------------------------

CREATE OR REPLACE PROCEDURE RecreateTempTable (
    p_description IN VARCHAR2) IS

   v_descrip       VARCHAR2(100) := p_description;
   v_cursor        NUMBER;
   v_createstring  VARCHAR2(100);
   v_dropstring    VARCHAR2(100);
   v_numrows       INTEGER;
BEGIN
   v_cursor := DBMS_SQL.OPEN_CURSOR;
   v_dropstring := 'DROP TABLE students_12345';

  BEGIN
  -- parse the query using the parameter table name
    DBMS_SQL.PARSE(v_cursor, v_dropString, DBMS_SQL.NATIVE);
  -- execute the cursor query
    v_numrows := DBMS_SQL.EXECUTE(v_cursor);
  EXCEPTION
    WHEN OTHERS THEN
       IF SQLCODE != -942 THEN
         RAISE;
       END IF;
  END;

  v_createstring := 'CREATE TABLE students_12345(s_id NUMBER, fname VARCHAR2(30),lname VARCHAR2(30),tname VARCHAR2(100), score NUMBER)';
  DBMS_SQL.PARSE(v_cursor, v_createstring, DBMS_SQL.NATIVE);
  v_numrows := DBMS_SQL.EXECUTE(v_cursor);

  DBMS_SQL.CLOSE_CURSOR(v_cursor);
EXCEPTION
  WHEN OTHERS THEN
     DBMS_SQL.CLOSE_CURSOR(v_cursor);
     RAISE;
END RecreateTempTable;
  /
-------------------------------------------------------------------------------
EXECUTE RecreateTempTable('a');
saggart
  • 25
  • 6
  • 1
    `v_createstring` is a `varchar2(100)`. It looks to me like the `create table` statement is probably a bit more than 100 characters. In this case, it would be easier to just use `execute immediate` for your dynamic SQL. `dbms_sql` comes in to play for more complicated situations where, for example, you want to run a query and iterate through the results without knowing the number of columns or their data types in advance. – Justin Cave Dec 01 '16 at 21:18
  • Oh god, that's amateur - i really should have spotted that!!! Thanks a lot Justin. I'm gonna take a look at execute_immediate tomorrow. – saggart Dec 01 '16 at 21:27

0 Answers0