Hi Everybody,
Here is the simplified sutiation. I have two tables, t1 and t1 having three similar (same data type) columns each. First two colunms serve as row identifiers, and the third column contain some value.
t1:
T1_C1 T1_C2 T1_C3
---------- ---------- ----------
1 1 1.1
1 2 1.2
1 3 1.3
2 1 2.1
2 2 2.2
2 3 2.3
3 1 3.1
3 2 3.2
3 3 3.3
t2:
T2_C1 T2_C2 T2_C3
---------- ---------- ----------
1 2 1.2
2 2 2.2
3 2 3.2
I am writing a procedure to fill in t2 with rows from t1 (only the rows which are absent in t2).
Here it is:
I fetch a row from the source table t1 in line 41, then in lines 48-50 I compile a SELECT query to check whether such a record exits in the the target table (t2). That's where I am getting an error at runtime:
SQL> execute p('t2','t2_c2','t2_c3','t1_c3');
SELECT count(t2_c1) INTO nm_L_n_of_records FROM t2 WHERE t2_c1=1 AND t2_c2=1
BEGIN p('t2','t2_c2','t2_c3','t1_c3'); END;
*
ERROR at line 1:
ORA-00905: missing keyword
ORA-06512: at "OPIS_RSRC.P", line 87
ORA-06512: at line 1
The resolved SELECT statement looks good to me, I can't get what that 'missing keyword' might be.
Can anyone help?
Thanks,
Alex
Here is the simplified sutiation. I have two tables, t1 and t1 having three similar (same data type) columns each. First two colunms serve as row identifiers, and the third column contain some value.
t1:
T1_C1 T1_C2 T1_C3
---------- ---------- ----------
1 1 1.1
1 2 1.2
1 3 1.3
2 1 2.1
2 2 2.2
2 3 2.3
3 1 3.1
3 2 3.2
3 3 3.3
t2:
T2_C1 T2_C2 T2_C3
---------- ---------- ----------
1 2 1.2
2 2 2.2
3 2 3.2
I am writing a procedure to fill in t2 with rows from t1 (only the rows which are absent in t2).
Here it is:
Code:
1 CREATE OR REPLACE PROCEDURE p
2 (
3 vc_in_t2_name in VARCHAR2,
4 vc_in_t2_col2_name in VARCHAR2,
5 vc_in_t2_col3_name in VARCHAR2,
6 vc_in_t1_col3_name in VARCHAR2
7 )
8
9 AS
10
11 vc_L_SQL_str VARCHAR2(1024);
12 nm_L_t1_c1 NUMBER(2);
13 nm_L_t1_c2 NUMBER(2);
14 nm_L_t1_c3 NUMBER(3,1);
15 nm_L_n_of_records NUMBER;
16
17 nm_crs_t1 NUMBER;
18 nm_tmp NUMBER;
19
20
21 BEGIN
22
23
24 vc_L_SQL_str := 'SELECT t1_c1,t1_c2,t1_c3 FROM t1';
25
26 --DBMS_OUTPUT.put_line(vc_L_SQL_str); --debug
27
28 nm_crs_t1 := DBMS_SQL.OPEN_CURSOR;
29 DBMS_SQL.PARSE(nm_crs_t1,vc_L_SQL_str,DBMS_SQL.NATIVE);
30
31 DBMS_SQL.DEFINE_COLUMN(nm_crs_t1, 1, nm_L_t1_c1);
32 DBMS_SQL.DEFINE_COLUMN(nm_crs_t1, 2, nm_L_t1_c2);
33 DBMS_SQL.DEFINE_COLUMN(nm_crs_t1, 3, nm_L_t1_c3);
34
35 nm_tmp := DBMS_SQL.EXECUTE(nm_crs_t1);
36
37
38 LOOP
39
40 -- read a line from t1
41 IF DBMS_SQL.FETCH_ROWS(nm_crs_t1) > 0 THEN
42
43 DBMS_SQL.COLUMN_VALUE(nm_crs_t1, 1, nm_L_t1_c1);
44 DBMS_SQL.COLUMN_VALUE(nm_crs_t1, 2, nm_L_t1_c2);
45 DBMS_SQL.COLUMN_VALUE(nm_crs_t1, 3, nm_L_t1_c3);
46
47 -- checks if such a record exists in t2
48 vc_L_SQL_str := 'SELECT count(t2_c1) INTO nm_L_n_of_records FROM ' ||
49 vc_in_t2_name || ' WHERE t2_c1=' || TO_CHAR(nm_L_t1_c1) ||
50 ' AND ' || vc_in_t2_col2_name || '=' || TO_CHAR(nm_L_t1_c2);
51
52 DBMS_OUTPUT.put_line(vc_L_SQL_str); --debug
53 EXECUTE IMMEDIATE(vc_L_SQL_str);
54
55 -- if value in source tbl exists and no such record in target table yet
56 IF nm_L_t1_c3 IS NOT NULL AND nm_L_n_of_records = 0 THEN
57
58 DBMS_OUTPUT.put_line('New record for #: ' || TO_CHAR(nm_L_t1_c1) || ',' || TO_CHAR(nm_L_t1_c2)); --debug
59
60 vc_L_SQL_str := 'INSERT INTO ' || vc_in_t2_name ||
61 ' (t2_c1, ' || vc_in_t2_col2_name || ',' || vc_in_t2_col3_name ||
62 ') VALUES (' || TO_CHAR(nm_L_t1_c1) || ',' || TO_CHAR(nm_L_t1_c2) ||
63 ',' || TO_CHAR(nm_L_t1_c3);
64
65 DBMS_OUTPUT.put_line(vc_L_SQL_str); --debug
66 EXECUTE IMMEDIATE(vc_L_SQL_str);
67 END IF;
68
69 ELSE
70
71 -- No more rows to copy:
72 EXIT;
73
74 END IF;
75
76 END LOOP;
77
78 DBMS_SQL.CLOSE_CURSOR(nm_crs_t1);
79
80 COMMIT;
81
82 EXCEPTION
83 WHEN OTHERS THEN
84 IF DBMS_SQL.IS_OPEN(nm_crs_t1) THEN
85 DBMS_SQL.CLOSE_CURSOR(nm_crs_t1);
86 END IF;
87 RAISE;
88
89 END p;
I fetch a row from the source table t1 in line 41, then in lines 48-50 I compile a SELECT query to check whether such a record exits in the the target table (t2). That's where I am getting an error at runtime:
SQL> execute p('t2','t2_c2','t2_c3','t1_c3');
SELECT count(t2_c1) INTO nm_L_n_of_records FROM t2 WHERE t2_c1=1 AND t2_c2=1
BEGIN p('t2','t2_c2','t2_c3','t1_c3'); END;
*
ERROR at line 1:
ORA-00905: missing keyword
ORA-06512: at "OPIS_RSRC.P", line 87
ORA-06512: at line 1
The resolved SELECT statement looks good to me, I can't get what that 'missing keyword' might be.
Can anyone help?
Thanks,
Alex