Solved

Missing Comma Error while inserting record dynamically

Posted on 2008-06-18
7
1,777 Views
Last Modified: 2008-06-18
Hi Experts,
I am trying to create a table dynamically and then insert data into it.
the CREATE command runs successfully while the INSERT errors out. It says that there are a few missing commas. Can someone help?
EXECUTE IMMEDIATE 'CREATE TABLE ' || TBL_TABLE_NAME ||
                     '(INDICATOR_ID NUMBER(20),MASTER_INDICATOR_ID NUMBER(20),USER_ROLE_ID NUMBER(20), TREE_ORDER NUMBER(20), ORGANIZATION_UNIT VARCHAR2(100), USER_FIRST_NAME VARCHAR2(30), USER_LAST_NAME VARCHAR2(30), HIERARCHY_LEVEL VARCHAR2(100), MAX_DEVIATION NUMBER(20,4), DEVIATION_TYPE VARCHAR2(1),UNIT_OF_EXPRESSION VARCHAR2(30), PLAN_VALUE NUMBER(20, 2),ACTUAL_VALUE NUMBER(20, 2), ACTION_PLAN VARCHAR2(1))';
 
   TREE_ORDER := 0;
   FOR I IN 1 .. RESULT_INDICATOR_RECORDSET.COUNT LOOP
   
      EXECUTE IMMEDIATE 'INSERT INTO ' || TBL_TABLE_NAME ||' VALUES('||RESULT_INDICATOR_RECORDSET(I)|| 
                        ' ,' ||RESULT_PARENT_RECORDSET(I)|| 
                        ' ,' ||RESULT_USER_RECORDSET(I)||
                        ' ,' ||TREE_ORDER||
                        ' ,' ||ORG_UNIT_RECORDSET(I)||
                        ' ,' ||USER_FIRST_NAME_RECORDSET(I)||
                        ' ,' ||USER_LAST_NAME_RECORDSET(I)||
                        ' ,' ||USER_ZONE_RECORDSET(I)||
                        ' ,' ||MAX_DEVIATION_RECORDSET(I)||
                        ' ,' ||DEVIATION_TYPE_RECORDSET(I)||
                        ' ,' ||UOM_TYPE_RECORDSET(I)||
                        ' ,' ||PLAN_VALUE_RECORDSET(I)||
                        ' ,' ||ACTUAL_VALUE_RECORDSET(I)||
                        ' ,' ||ACTION_PLAN_RECORDSET(I)||')';

Open in new window

0
Comment
Question by:110210
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:Ritesh_Garg
ID: 21817641
Try this:
EXECUTE IMMEDIATE 'CREATE TABLE ' || TBL_TABLE_NAME ||
                     '(INDICATOR_ID NUMBER(20),MASTER_INDICATOR_ID NUMBER(20),USER_ROLE_ID NUMBER(20), TREE_ORDER NUMBER(20), ORGANIZATION_UNIT VARCHAR2(100), USER_FIRST_NAME VARCHAR2(30), USER_LAST_NAME VARCHAR2(30), HIERARCHY_LEVEL VARCHAR2(100), MAX_DEVIATION NUMBER(20,4), DEVIATION_TYPE VARCHAR2(1),UNIT_OF_EXPRESSION VARCHAR2(30), PLAN_VALUE NUMBER(20, 2),ACTUAL_VALUE NUMBER(20, 2), ACTION_PLAN VARCHAR2(1))';
 
   TREE_ORDER := 0;
   FOR I IN 1 .. RESULT_INDICATOR_RECORDSET.COUNT LOOP
   
      EXECUTE IMMEDIATE 'INSERT INTO ' || TBL_TABLE_NAME ||' VALUES('||NVL(RESULT_INDICATOR_RECORDSET(I),'NULL')|| 
                        ' ,' ||NVL(RESULT_PARENT_RECORDSET(I),'NULL')|| 
                        ' ,' ||NVL(RESULT_USER_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(TREE_ORDER,'NULL')||
                        ' ,' ||NVL(ORG_UNIT_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_FIRST_NAME_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_LAST_NAME_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_ZONE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(MAX_DEVIATION_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(DEVIATION_TYPE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(UOM_TYPE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(PLAN_VALUE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(ACTUAL_VALUE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(ACTION_PLAN_RECORDSET(I),'NULL')||')';

Open in new window

0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 21817677
what is the actual error?  "a few missing commas"  is not an oracle error message  :)

in the mean time,  try rewriting the inserts like this.  

       EXECUTE IMMEDIATE 'INSERT INTO ' || tbl_table_name
                      || ' VALUES(:b1,:b2,:b3,:b4,:b5,:b6,:b7,:b8,:b9,:b10,:b11,:b12,:b13,:b14)'
            USING result_indicator_recordset(i), result_parent_recordset(i),
                  result_user_recordset(i), tree_order, org_unit_recordset(i),
                  user_first_name_recordset(i), user_last_name_recordset(i),
                  user_zone_recordset(i), max_deviation_recordset(i),
                  deviation_type_recordset(i), uom_type_recordset(i),
                  plan_value_recordset(i), actual_value_recordset(i),
                  action_plan_recordset(i);
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 21817689
the reason I changed it to use the "USING" is even though your statement is dynamic,  you can still take advantage of bind variables.  And, the more times you loop, the more effective USING bind variables will be.

Plus, it makes the statement easier to debug since there is only one dynamic piece left in it now.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:110210
ID: 21817741
No. It doesn't seem to work. It says trying to insert string for numeric values, when I change all the 'NULL' to either '' or 0..it reverts back to the old error
0
 
LVL 14

Expert Comment

by:ajexpert
ID: 21817757
Hi,
You can easily findout which statement is erroring out.
Here is some addition to your existing code
  lv_sql  VARCHAR2(4000);
  
  lv_sql  :=  'INSERT INTO ' || TBL_TABLE_NAME ||' VALUES('||NVL(RESULT_INDICATOR_RECORDSET(I),'NULL')|| 
                        ' ,' ||NVL(RESULT_PARENT_RECORDSET(I),'NULL')|| 
                        ' ,' ||NVL(RESULT_USER_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(TREE_ORDER,'NULL')||
                        ' ,' ||NVL(ORG_UNIT_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_FIRST_NAME_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_LAST_NAME_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(USER_ZONE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(MAX_DEVIATION_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(DEVIATION_TYPE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(UOM_TYPE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(PLAN_VALUE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(ACTUAL_VALUE_RECORDSET(I),'NULL')||
                        ' ,' ||NVL(ACTION_PLAN_RECORDSET(I),'NULL')||')';
 
 
 BEGIN
 EXECUTE IMMEDIATE lv_sql;
 EXCEPTION
 WHEN OTHERS THEN
   DBMS_OUTPUT.PUT_LINE lv_sql;
 END;   

Open in new window

0
 

Author Comment

by:110210
ID: 21817787
thanks all for the help.

sdstuber, your solution worked(and still works) well.
The actual error was ORA-00917 missing comma.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 21819088
glad I could help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ORA-00923: FROM keyword not found where expected 3 95
dbms_crypto.decrypt   errors out 6 47
Oracle collections 15 39
Oracle Nested table uses ? 2 45
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question