PL / SQL动态INSERT

时间:2017-06-01 15:26:17

标签: sql oracle plsql

我有一个TABLE_A,它包含一个以逗号分隔值作为数据的列。现在我必须将这些逗号分隔值放入250列的表B中,这必须动态完成吗?

1 个答案:

答案 0 :(得分:0)

这是一个快速而又脏的脚本,可以帮助您实现目标

SQL> create table T ( c clob );

Table created.

SQL>
SQL> create table t1 ( c1 varchar2(50) );

Table created.

SQL> begin
  2  for i in  2 .. 250 loop
  3   execute immediate 'alter table t1 add c'||i||' varchar2(50)';
  4  end loop;
  5  end;
  6  /

PL/SQL procedure successfully completed.

SQL>
SQL> declare
  2    v clob := 'somedata1';
  3  begin
  4    for i in  2 .. 250 loop
  5      v := v || ',somedata'||i;
  6    end loop;
  7    insert into t
  8    select v from dual connect by level <= 2000;
  9  end;
 10  /

PL/SQL procedure successfully completed.

SQL>
SQL> set timing on
SQL> declare
  2    type t_cols is table of varchar2(100) index by pls_integer;
  3    l_cols t_cols;
  4
  5    l_comma pls_integer;
  6    l_text varchar2(32767);
  7
  8    l_insert_sql varchar2(4000) := 'insert into t1 values (:1';
  9  begin
 10    for i in 2 .. 250 loop
 11       l_insert_sql := l_insert_sql || ',:'||i;
 12    end loop;
 13    l_insert_sql := l_insert_sql || ')';
 14
 15    for i in ( select rownum r, c from t ) loop
 16      l_text := i.c||',';
 17
 18      for x in 1 .. 250 loop
 19        l_comma := instr(l_text,',');
 20        l_cols(x)   := substr(l_text,1,l_comma-1);
 21        l_text := substr(l_text,l_comma+1);
 22      end loop;
 23
 24    execute immediate l_insert_sql
 25      using
 26         l_cols(1)
 27        ,l_cols(2)
 28        ,l_cols(3)
 29        ,l_cols(4)
 30        ,l_cols(5)
 31        ,l_cols(6)
 32        ,l_cols(7)
 33        ,l_cols(8)
 34        ,l_cols(9)
 35        ,l_cols(10)
 36        ,l_cols(11)
 37        ,l_cols(12)
 38        ,l_cols(13)
 39        ,l_cols(14)
 40        ,l_cols(15)
 41        ,l_cols(16)
 42        ,l_cols(17)
 43        ,l_cols(18)
 44        ,l_cols(19)
 45        ,l_cols(20)
 46        ,l_cols(21)
 47        ,l_cols(22)
 48        ,l_cols(23)
 49        ,l_cols(24)
 50        ,l_cols(25)
 51        ,l_cols(26)
 52        ,l_cols(27)
 53        ,l_cols(28)
 54        ,l_cols(29)
 55        ,l_cols(30)
 56        ,l_cols(31)
 57        ,l_cols(32)
 58        ,l_cols(33)
 59        ,l_cols(34)
 60        ,l_cols(35)
 61        ,l_cols(36)
 62        ,l_cols(37)
 63        ,l_cols(38)
 64        ,l_cols(39)
 65        ,l_cols(40)
 66        ,l_cols(41)
 67        ,l_cols(42)
 68        ,l_cols(43)
 69        ,l_cols(44)
 70        ,l_cols(45)
 71        ,l_cols(46)
 72        ,l_cols(47)
 73        ,l_cols(48)
 74        ,l_cols(49)
 75        ,l_cols(50)
 76        ,l_cols(51)
 77        ,l_cols(52)
 78        ,l_cols(53)
 79        ,l_cols(54)
 80        ,l_cols(55)
 81        ,l_cols(56)
 82        ,l_cols(57)
 83        ,l_cols(58)
 84        ,l_cols(59)
 85        ,l_cols(60)
 86        ,l_cols(61)
 87        ,l_cols(62)
 88        ,l_cols(63)
 89        ,l_cols(64)
 90        ,l_cols(65)
 91        ,l_cols(66)
 92        ,l_cols(67)
 93        ,l_cols(68)
 94        ,l_cols(69)
 95        ,l_cols(70)
 96        ,l_cols(71)
 97        ,l_cols(72)
 98        ,l_cols(73)
 99        ,l_cols(74)
100        ,l_cols(75)
101        ,l_cols(76)
102        ,l_cols(77)
103        ,l_cols(78)
104        ,l_cols(79)
105        ,l_cols(80)
106        ,l_cols(81)
107        ,l_cols(82)
108        ,l_cols(83)
109        ,l_cols(84)
110        ,l_cols(85)
111        ,l_cols(86)
112        ,l_cols(87)
113        ,l_cols(88)
114        ,l_cols(89)
115        ,l_cols(90)
116        ,l_cols(91)
117        ,l_cols(92)
118        ,l_cols(93)
119        ,l_cols(94)
120        ,l_cols(95)
121        ,l_cols(96)
122        ,l_cols(97)
123        ,l_cols(98)
124        ,l_cols(99)
125        ,l_cols(100)
126        ,l_cols(101)
127        ,l_cols(102)
128        ,l_cols(103)
129        ,l_cols(104)
130        ,l_cols(105)
131        ,l_cols(106)
132        ,l_cols(107)
133        ,l_cols(108)
134        ,l_cols(109)
135        ,l_cols(110)
136        ,l_cols(111)
137        ,l_cols(112)
138        ,l_cols(113)
139        ,l_cols(114)
140        ,l_cols(115)
141        ,l_cols(116)
142        ,l_cols(117)
143        ,l_cols(118)
144        ,l_cols(119)
145        ,l_cols(120)
146        ,l_cols(121)
147        ,l_cols(122)
148        ,l_cols(123)
149        ,l_cols(124)
150        ,l_cols(125)
151        ,l_cols(126)
152        ,l_cols(127)
153        ,l_cols(128)
154        ,l_cols(129)
155        ,l_cols(130)
156        ,l_cols(131)
157        ,l_cols(132)
158        ,l_cols(133)
159        ,l_cols(134)
160        ,l_cols(135)
161        ,l_cols(136)
162        ,l_cols(137)
163        ,l_cols(138)
164        ,l_cols(139)
165        ,l_cols(140)
166        ,l_cols(141)
167        ,l_cols(142)
168        ,l_cols(143)
169        ,l_cols(144)
170        ,l_cols(145)
171        ,l_cols(146)
172        ,l_cols(147)
173        ,l_cols(148)
174        ,l_cols(149)
175        ,l_cols(150)
176        ,l_cols(151)
177        ,l_cols(152)
178        ,l_cols(153)
179        ,l_cols(154)
180        ,l_cols(155)
181        ,l_cols(156)
182        ,l_cols(157)
183        ,l_cols(158)
184        ,l_cols(159)
185        ,l_cols(160)
186        ,l_cols(161)
187        ,l_cols(162)
188        ,l_cols(163)
189        ,l_cols(164)
190        ,l_cols(165)
191        ,l_cols(166)
192        ,l_cols(167)
193        ,l_cols(168)
194        ,l_cols(169)
195        ,l_cols(170)
196        ,l_cols(171)
197        ,l_cols(172)
198        ,l_cols(173)
199        ,l_cols(174)
200        ,l_cols(175)
201        ,l_cols(176)
202        ,l_cols(177)
203        ,l_cols(178)
204        ,l_cols(179)
205        ,l_cols(180)
206        ,l_cols(181)
207        ,l_cols(182)
208        ,l_cols(183)
209        ,l_cols(184)
210        ,l_cols(185)
211        ,l_cols(186)
212        ,l_cols(187)
213        ,l_cols(188)
214        ,l_cols(189)
215        ,l_cols(190)
216        ,l_cols(191)
217        ,l_cols(192)
218        ,l_cols(193)
219        ,l_cols(194)
220        ,l_cols(195)
221        ,l_cols(196)
222        ,l_cols(197)
223        ,l_cols(198)
224        ,l_cols(199)
225        ,l_cols(200)
226        ,l_cols(201)
227        ,l_cols(202)
228        ,l_cols(203)
229        ,l_cols(204)
230        ,l_cols(205)
231        ,l_cols(206)
232        ,l_cols(207)
233        ,l_cols(208)
234        ,l_cols(209)
235        ,l_cols(210)
236        ,l_cols(211)
237        ,l_cols(212)
238        ,l_cols(213)
239        ,l_cols(214)
240        ,l_cols(215)
241        ,l_cols(216)
242        ,l_cols(217)
243        ,l_cols(218)
244        ,l_cols(219)
245        ,l_cols(220)
246        ,l_cols(221)
247        ,l_cols(222)
248        ,l_cols(223)
249        ,l_cols(224)
250        ,l_cols(225)
251        ,l_cols(226)
252        ,l_cols(227)
253        ,l_cols(228)
254        ,l_cols(229)
255        ,l_cols(230)
256        ,l_cols(231)
257        ,l_cols(232)
258        ,l_cols(233)
259        ,l_cols(234)
260        ,l_cols(235)
261        ,l_cols(236)
262        ,l_cols(237)
263        ,l_cols(238)
264        ,l_cols(239)
265        ,l_cols(240)
266        ,l_cols(241)
267        ,l_cols(242)
268        ,l_cols(243)
269        ,l_cols(244)
270        ,l_cols(245)
271        ,l_cols(246)
272        ,l_cols(247)
273        ,l_cols(248)
274        ,l_cols(249)
275        ,l_cols(250);
276
277    end loop;
278
279  end;
280  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:01.11
SQL>