我在名为mat-spinner {
zoom: 0.2
}
和Query
的表中有2列。
查询:
Values
值: insert into
tbl_details(CName,Line,Type,Command,Rule,Client_ID,Site_ID,SName)
values (l_Name,:l_Line,:l_Type,:l_Command,:l_Rule,:l_Client_ID,:l_Site_ID,l_Name)
CName和SName使用相同的值#1(2):20 #2(1):H #3(2):IF #4(27):FA - RETAIN OLD MASTER DATA #5(0): #6(0):
。
我想用下一列中的值替换绑定的变量,以获取可执行查询。
我想要什么: 我想编写一个从表的两列获取值的过程来进行查询并执行该查询。
Manu
答案 0 :(得分:1)
这里是一个选项,它将查询VALUES
子句以及 values 本身转换为行-每个都在其自己的光标循环中,通过其行号将绑定变量及其值配对。 CHR(39)
是单引号。
由于该表很可能不包含一行(是否由某个ID标识?您从未告诉过我们),因此您必须对其进行调整,否则它将无法正常工作。
测试表:
SQL> select * from test;
QUERY
--------------------------------------------------------------------------------
C_VALUES
--------------------------------------------------------------------------------
insert into
tbl_details(CName,Line,Type,Command,Rule,Client_ID,Site_ID,SName)
values (l_Name,:l_Line,:l_Type,:l_Command,:l_Rule,:l_Client_ID,:l_Site_ID,l_Name
)
#1(2):20 #2(1):H #3(2):IF #4(27):FA - RETAIN OLD MASTER DATA #5(0): #6(0):
SQL> set serveroutput on
代码和结果:
SQL> DECLARE
2 l_query test.query%TYPE;
3 l_name VARCHAR2 (30) := 'Manu';
4 BEGIN
5 SELECT query INTO l_query FROM test;
6
7 -- bind variables in QUERY
8 FOR cur_l
9 IN (SELECT ROW_NUMBER () OVER (ORDER BY lvl) rn, res l_val
10 FROM ( SELECT LEVEL lvl,
11 REGEXP_SUBSTR (res,
12 '[^,]+',
13 1,
14 LEVEL)
15 res
16 FROM (SELECT SUBSTR (query, INSTR (query, 'values')) res
17 FROM test)
18 CONNECT BY LEVEL <= REGEXP_COUNT (res, ':') + 1)
19 WHERE SUBSTR (res, 1, 1) = ':')
20 LOOP
21 -- values in VALUES
22 FOR cur_v
23 IN (SELECT rn, TRIM (SUBSTR (res, INSTR (res, ':') + 1)) c_val
24 FROM ( SELECT LEVEL rn,
25 REGEXP_SUBSTR (t.c_values,
26 '[^#]+',
27 1,
28 LEVEL)
29 res
30 FROM test t
31 CONNECT BY LEVEL <= REGEXP_COUNT (t.c_values, '#'))
32 WHERE rn = cur_l.rn)
33 LOOP
34 l_query :=
35 REPLACE (l_query,
36 cur_l.l_val,
37 CHR (39) || cur_v.c_val || CHR (39));
38 END LOOP;
39 END LOOP;
40
41 -- Put Manu into l_Name
42 l_query := REPLACE (l_query, 'l_Name', CHR (39) || l_name || CHR (39));
43
44 DBMS_OUTPUT.put_line (l_query);
45 END;
46 /
insert into
tbl_details(CName,Line,Type,Command,Rule,Client_ID,Site_ID,SName)
values ('Manu','20','H','IF','FA - RETAIN OLD MASTER DATA','','','Manu')
PL/SQL procedure successfully completed.
SQL>