-
Notifications
You must be signed in to change notification settings - Fork 18
/
sqldeclaration.html
853 lines (675 loc) · 26.2 KB
/
sqldeclaration.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
<!-- Creator : groff version 1.22.3 -->
<!-- CreationDate: Mon Mar 18 08:51:06 2024 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>sqldeclaration</title>
</head>
<body>
<h1 align="center">sqldeclaration</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNTAX">SYNTAX</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#SCANNING FOR SQL OR PL/SQL">SCANNING FOR SQL OR PL/SQL</a><br>
<a href="#STATIC SQL">STATIC SQL</a><br>
<a href="#DYNAMIC SQL">DYNAMIC SQL</a><br>
<a href="#ATTRIBUTES">ATTRIBUTES</a><br>
<a href="#IMPLICIT BIND/DEFINE">IMPLICIT BIND/DEFINE</a><br>
<a href="#IMMEDIATE SQL">IMMEDIATE SQL</a><br>
<a href="#EMBEDDED SQL">EMBEDDED SQL</a><br>
<a href="#NULL">NULL</a><br>
<a href="#EXAMPLES">EXAMPLES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#COPYRIGHT">COPYRIGHT</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">sqldeclaration
− RWP*Load Simulator declaration of SQL statements</p>
<h2>SYNTAX
<a name="SYNTAX"></a>
</h2>
<pre style="margin-left:11%; margin-top: 1em">sqldeclaration ::=
<b>sql</b> identifier
[ sqltext ]
{ sqlspecification<b> ;</b> }
<b>end</b> [<b> sql</b> | identifier ]
immediatesql ::=
<b>sql execute
</b> sqltext
{ sqlspecification<b> ;</b> }
<b>end</b> [<b> sql</b> ]
sqltext ::=
embeddedsqltext
|<b> file</b> concatenation<b> ;
</b>| concatenation<b> ;
</b>embeddedsqltext ::=
SQLtext<b> ;</b> newline
| SQLtext newline whitespace (<b> /</b> |<b> .</b> ) newline
| PLSQLblock newline whitespace (<b> /</b> |<b> .</b> ) newline
sqlspecification ::=
<b> bind</b> bindspecification {<b> ,</b> bindspecification }
|<b> bindout</b> bindspecification {<b> ,</b> bindspecification }
|<b> define</b> integer identifier {<b> ,</b> integer identifier }
|<b> bind sql
</b>|<b> define sql
</b>|<b> array</b> [<b> define</b> ] expression
|<b> ignoreerror
</b>|<b> cursorcache
</b>|<b> nocursorcache
</b>bindspecification ::=
| string identifier
| integer identifier
|<b> :</b> identifier
SQLtext ::=
PLSQLblock ::=</pre>
<p style="margin-left:11%; margin-top: 1em">See below under
SCANNING.</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">In rwloadsim,
you can include SQL statements in three different ways:</p>
<p style="margin-left:11%; margin-top: 1em">The most simple
method, called <b>embedded sql</b> can be used if you need
to execute single row SQL, typically dml, ddl or single row
queries, or if you need to execute some query as a cursor
loop. You simply include the SQL text directly in your rwl
program. The following constraints apply to embedded
sql:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>1.</p></td>
<td width="3%"></td>
<td width="83%">
<p><i>All</i> bind variables must exist as variables of the
same name, and bindout will be done only if the
<b>$bindoutname:on</b> directive is in effect.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>2.</p></td>
<td width="3%"></td>
<td width="83%">
<p>For both single row queries and cursor loop, <i>all</i>
select list elements must have simple names or have an alias
as a simple name, and these must exist as variables.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>3.</p></td>
<td width="3%"></td>
<td width="83%">
<p>No specifications such as array size can be provided;
however, see below for directives setting array sizes for
embedded sql.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>4.</p></td>
<td width="3%"></td>
<td width="83%">
<p>The sql is anonymous and cannot be used with any
<b>modify sql</b> statement as these require the name of the
sql.</p> </td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The second
approach is called <b>immediate sql</b> which is still
anonymous but without the first three restrictions of
embedded sql. In your code, you initiate immediate sql with
the keywords <b>sql execute</b> and it is finished with the
keyword <b>end</b>.</p>
<p style="margin-left:11%; margin-top: 1em">The third
method was the only one available in earlier versions of
rwloadsim. Using this method, you first declare the sql
statement as a variable, which subsequently can be used in
several different places. The typical use of a SQL variable
is to execute it as such, which normally is done for DML,
DDL, PL/SQL or single row queries, or if it is a query
without a known number of rows to execute it in a cursor
loop. Using a variable is suggested if you need to execute
the same SQL from different places in your rwloadsim
program.</p>
<p style="margin-left:11%; margin-top: 1em">The <b>sql</b>
keyword followed by an identifier initiates a declaration of
a static or dynamic sql statement and the declaration
potentially provides a list of variables used for bind,
define and bindout in addition to several other attributes.
Each such attribute (such as bind) must be terminated by a
;. and the complete declaration must be finished by the
keyword <b>end</b> which can be optionally followed by
<b>sql</b> or by the name of the sql.</p>
<h2>SCANNING FOR SQL OR PL/SQL
<a name="SCANNING FOR SQL OR PL/SQL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The RWP*Load
Simulator recognizes keywords or sequences of multiple
keywords that initiate SQL or PL/SQL and if these are seen,
SQL or PL/SQL scan will be started. As opposed to anything
else in rwloadsim, this scan is not case sensitive.</p>
<p style="margin-left:11%; margin-top: 1em">The following
list all keywords or sequences of keywords that initiate
scan for PL/SQL, which implies the scan finishes by a line
with white space and a / or . at the end of the line.</p>
<p style="margin-left:17%; margin-top: 1em"><b>declare <br>
begin <br>
-- <br>
create</b> [ <b>or replace</b> ] <b>procedure <br>
create</b> [ <b>or replace</b> ] <b>function <br>
create</b> [ <b>or replace</b> ] <b>package <br>
create</b> [ <b>or replace</b> ] <b>library <br>
create</b> [ <b>or replace</b> ] <b>trigger <br>
create</b> [ <b>or replace</b> ] <b>java</b></p>
<p style="margin-left:11%; margin-top: 1em">The following
lists all keywords or sequences of keywords that initiate
scan for SQL, which implies the scan additionally finishes
by a ; at the end of a line.</p>
<p style="margin-left:17%; margin-top: 1em"><b>select <br>
delete <br>
update <br>
insert <br>
create</b> (except those mentioned above) <b><br>
drop <br>
alter <br>
/* <br>
administer key management <br>
analyze <br>
associate statistics <br>
audit <br>
comment on <br>
disassociate statistics <br>
explain plan <br>
flashback database <br>
flashback table <br>
grant <br>
lock table <br>
noaudit <br>
purge <br>
rename <br>
revoke <br>
savepoint <br>
set transaction <br>
set constraint <br>
set role <br>
with</b></p>
<p style="margin-left:11%; margin-top: 1em">The two comment
keywords <b>/*</b> and <b>--</b> will initiate scanning for
respectively SQL or PL/SQL even if they are not immediately
followed by white space.</p>
<p style="margin-left:11%; margin-top: 1em">As opposed to
SQL*Plus, rwloadsim does not terminate scanning for SQL or
PL/SQL at an empty line.</p>
<p style="margin-left:11%; margin-top: 1em">Rwloadsim does
not make any modifications to either, so the complete text
including any newline, white space, etc from the initial
keyword and until the terminator becomes the text that
eventually will be the argument to OCIStmtPrepare2.</p>
<p style="margin-left:11%; margin-top: 1em">If you have a
need for comments inside the SQL or PL/SQL, use the
appropriate syntax from those languages, i.e. -- or /* */; a
# will be part of the SQL or PL/SQL text.</p>
<h2>STATIC SQL
<a name="STATIC SQL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">You declare a
static sql variable using the keyword <b>sql</b> followed by
the name of the variable, and the actual text of the SQL or
PL/SQL must follow. There are three ways to provide the SQL
text:</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p style="margin-top: 1em">1.</p></td>
<td width="3%"></td>
<td width="83%">
<p style="margin-top: 1em">Have rwloadsim scan for SQL or
PL/SQL similar to how SQL*Plus does it. This is the
preferred approach.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>2.</p></td>
<td width="3%"></td>
<td width="83%">
<p>Provide the SQL or PL/SQL text in a named file. As an
alternative to embed your SQL or PL/SQL text directly in
your rwl source file you can use the <b>file</b> keyword
followed by a concatenation that resolves to a file name.
The file will be opened and the SQL or PL/SQL text will be
read from it; the file should not have either of the
terminators ; . /.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="3%">
<p>3.</p></td>
<td width="3%"></td>
<td width="83%">
<p>Have a concatenation (including just a string constant)
that is the full sql text. This is primarily useful if the
full SQL or PL/SQL text is created from concatenating
several text strings or variables containing text strings.
Note that local variables cannot be used for this, and that
the value is a static, compile time value. If you need sql
inside a procedure that contains values from expressions,
use the dynamic sql approach of rwloadsim. Also note that if
you use this approach, any " needed in your sql text
must be prefixed by \ for it to become part of a string in
rwloadsim.</p> </td></tr>
</table>
<h2>DYNAMIC SQL
<a name="DYNAMIC SQL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">If there is no
SQL or PL/SQL provided, neither directly in your rwl source
file nor being read from a file, the declared sql statement
is dynamic and the text must be provided using the <b>modify
sql</b> command. Note that the rwl language (presently) does
not have a mechanism to <i>describe</i> the actually
provided SQL statement. You can neither get the type of the
sql (e.g. query or DML), nor the list of select list
elements or bind variables. During declaration of a dynamic
sql, you can provide bind, bindout or define variables that
are known to be present for all executions regardless of the
actual SQL text. During later execution, you can add extra
variables as needed using the <b>modify sql</b> command.</p>
<h2>ATTRIBUTES
<a name="ATTRIBUTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">In most cases,
you will need to provide attributes for your sql statement.
These include variables use for bind and/or define, array
sizes, use of the cursor cache, etc.</p>
<p style="margin-left:11%; margin-top: 1em"><b>bind
placeholder variable</b></p>
<p style="margin-left:17%;">Bind the named variable as
input to a placeholder in your sql text. The placeholder is
either an integer which results in bind-by-position or is a
text string which results in bind-by-name in which case the
first character of the text must be a colon. This can e.g.
be used in the values clause of an insert statement, or in
the where clause of a query.</p>
<p style="margin-left:17%; margin-top: 1em">You can replace
the placeholder by a single <b>:</b> in which case it will
be bind-by-name where both the name of the placeholder and
the name of the variable it is bound to is the identifier
following the colon.</p>
<p style="margin-left:11%; margin-top: 1em"><b>bindout
placeholder variable</b></p>
<p style="margin-left:17%;">Bind the named variable as
output from a placeholder in your sql text. This can e.g. be
used as an output variable in a PL/SQL block or in the
returning clause of an insert statement. Bindout cannot be
used in combination with the array interface. As for normal
bind, you can use bind-by-position or bind-by-name and you
can replace the placeholder with <b>:</b>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>define
position variable</b></p>
<p style="margin-left:17%;">Define the named variable as
select-list-element of a query. The integer position is the
number in the select list, and the call results in a
define-by-position.</p>
<p style="margin-left:11%; margin-top: 1em"><b>bind sql
<br>
define sql</b></p>
<p style="margin-left:17%;">These options will turn on
implicit bind respectively define for the SQL statement;
both can be used as needed. See below under IMPLICIT
BIND/DEFINE for details.</p>
<p style="margin-left:11%; margin-top: 1em"><b>array
size</b></p>
<p style="margin-left:17%;">Set the number of rows in the
array interface. For cursor loops, the default is to use an
array sized by memory, and for anything else, the default is
1. For cursor loops, the array interface is handled using
the features of Oracle Call Interface, unless you use the
<b>array define</b> keywords in which case the array is
allocated by rwloadsim. For DML, the array interface is
allocated by rwloadsim and is automatically flushed as
needed during commit or rollback. You can flush the array
for DML explicitly using a <b>modify sql array execute</b>
statement.</p>
<p style="margin-left:11%; margin-top: 1em"><b>ignoreerror</b></p>
<p style="margin-left:17%;">Prevent writing any error
during execution of the sql statement to stderr. You should
code error handling yourself using the <b>oraerror</b>
and/or <b>oraerrortext</b> variables. Other error processing
such as insert into the <b>oerstats</b> table still takes
place.</p>
<p style="margin-left:11%; margin-top: 1em"><b>nocursorcache</b></p>
<p style="margin-left:17%;">By default, rwloadsim will use
the cursor cache provided by Oracle Call Interface. It may
be beneficial to turn this off for sql that is only executed
once.</p>
<h2>IMPLICIT BIND/DEFINE
<a name="IMPLICIT BIND/DEFINE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Typically, you
will use <b>bind</b> and/or <b>define</b> to associate
variables in your rwl program with bind-variables (a.k.a.
placeholders) or select list elements. To ease your rwl
program writing, you can have rwloadsim do this implicitly,
which it does by matching names of placeholders or select
list elements in your SQL statement to declared variables in
your rwl program. If a match is found, the mapping will be
done and the appropriate bind or define is handled without
an explicit <b>bind</b> and/or <b>define</b>.</p>
<p style="margin-left:11%; margin-top: 1em">This implicit
matching can be specified as part of your SQL statement
declaration or it can be enabled generally via directives.
The implicit matching takes place once during the first
execution of your SQL statement, and the binds and/or
defines done will subsequently be used for any subsequent
execution. The name of the select list element or bind
variable found in the SQL statement will be converted to
lowercase before matching to a variable name. This mechanism
can be turned off using the <b>$implicit:keepcase</b>
directive.</p>
<p style="margin-left:11%; margin-top: 1em">If your SQL
statement has bind variables (e.g. :1, :2, etc) or select
list elements (e.g. expressions), that cannot possibly be
matched to any identifiers, you need to either use explicit
bind or define or to replace a bind variable by a proper
name or give an alias to the select list element.</p>
<p style="margin-left:11%; margin-top: 1em">Any bindout
required will be done if the <b>$bindoutname:on</b>
directive is in effect, otherwise it must be explicitly
done. Implicit define cannot be used in combination with
array define.</p>
<p style="margin-left:11%; margin-top: 1em">You can combine
explicit and implicit bind/define, in which case the
implicit behavior will only apply to those not already done
explicitly. It is, however, suggested you do not combine the
two.</p>
<p style="margin-left:11%; margin-top: 1em">Queries with
implicit define and without an explicit <b>array</b> will
use OCI prefetch based on memory set to 100kB.</p>
<p style="margin-left:11%; margin-top: 1em">You can also
use implicit bind/define with dynamic SQL, in which case any
implicitly created binds and/or defines are released when
<b>modify sql release</b> is being performed.</p>
<h2>IMMEDIATE SQL
<a name="IMMEDIATE SQL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">In version 3 of
rwloadsim, you can use the keyword <b>execute</b> rather
than an identifier, causing your SQL to be unnamed and
immediately executed. This simplifies your rwloadsim code as
you do not need to separate the declaration and the
execution of your SQL statements. Additionally, implicit
bind and define will be enabled by default for immediate
sql.</p>
<p style="margin-left:11%; margin-top: 1em">The immediate
sql syntax can be used stand alone to execute single row
queries, DDL, DML or PL/SQL, or it can be used as part of a
cursor loop.</p>
<p style="margin-left:11%; margin-top: 1em">If bindout is
needed, you can use the <b>$bindoutname:on</b> directive,
which causes rwloadsim to inspect the first characters of
the placeholder, and if these match, the bind will be a
bindout. The only specification you typically would need is
<b>array</b> to enable the array interface for DML. This
cannot be used if bindout is in effect.</p>
<p style="margin-left:11%; margin-top: 1em">The sql text
for immediate sql can be a concatenation, which is
calculated at runtime, effectively implying dynamically
generated sql. However, as rwloadsim does not have a
possibility for the programmer to <i>describe</i> the sql to
find select list elements and/or bind variables, these must
be "known" to the programmer. As an example, using
’select *’ in a query requires the programmer to
know what the asterisk expands to and therefore which define
variables to declare.</p>
<h2>EMBEDDED SQL
<a name="EMBEDDED SQL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Version 3 of
rwloadsim also has the possibility to simply embed static
sql directly without the need for the <b>sql</b> and
<b>end</b> keywords to wrap the sql and any specifications
needed. Due to the missing keywords, you cannot provide any
specifications, which also means you cannot set the array
size. You can set a default array sizes using either of the
directives <b>$embeddedqueryarray</b> or
<b>$embeddeddmlarray</b>. The default for queries in cursor
loops is to use OCI memory based array and for dml to not
use array.</p>
<p style="margin-left:11%; margin-top: 1em">If the
<b>$bindoutname:on</b> directive is in effect, placeholder
names where the first characters match the letters of the
directive will be used as bindout.</p>
<h2>NULL
<a name="NULL"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Variables of
type integer or double can be null in rwloadsim, and that
property is being retained when such variables are used for
implicit or explicit <b>bind</b>, <b>bindout</b> or
<b>define</b>.</p>
<h2>EXAMPLES
<a name="EXAMPLES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>Simple
query</b></p>
<p style="margin-left:11%; margin-top: 1em">Declare some
variables and a simple sql statement using bind by name for
its bind variable, execute the query and print some
output:</p>
<pre style="margin-left:17%; margin-top: 1em">integer empno := 7900;
double sal;
string(20) ename;
sql getemp
select ename, sal from emp
where empno=:empno
/
bind ":empno" empno;
define 1 ename, 2 sal;
end;
getemp;
printline "Employee #" empno ", name:" ename ", salary:" sal;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Declaration
and use of cursor</b></p>
<pre style="margin-left:17%; margin-top: 1em">sql getemps
select e.ename, e.sal
, d.loc
from emp e join dept d
on e.deptno = d.deptno
where d.deptno = :1
and e.sal > :2;
bind 1 deptno; bind 2 sallimit;
define 1 ename, 2 sal;
define 3 location;
array 5;
end sql;
for getemps loop
printline ename, sal, loc;
end loop;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Implicit
bind and define</b></p>
<p style="margin-left:11%; margin-top: 1em">Declare a
procedure showing how implicit bind and define can be used.
Note that the alias for the select list element e.sal/12 is
<i>required</i> as there would otherwise be no simple name
that could be matched to a variable.</p>
<pre style="margin-left:17%; margin-top: 1em">$implicit:both
procedure printemps(integer deptno)
string ename, dname;
integer empno;
double monthsal;
sql getemps
select e.empno, e.ename, d.dname
, e.sal/12 monthsal
from emp e join dept d
on e.deptno = d.deptno
where d.deptno=:deptno
/
end;
for getemps loop
printline empno, ename, monthsal, dname;
end loop;
end printemps;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Immediate
execute of simple query</b></p>
<p style="margin-left:11%; margin-top: 1em">This is similar
to the first example using immediate sql execution.</p>
<pre style="margin-left:17%; margin-top: 1em">integer empno := 7900;
double sal;
string(20) ename;
sql execute
select ename, sal from emp
where empno=:empno
/
end;
printline "Employee #" empno ", name:" ename ", salary:" sal;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Cursor loop
with embedded sql</b></p>
<p style="margin-left:11%; margin-top: 1em">Declare a
procedure that uses embedded sql to print employees in some
department.</p>
<pre style="margin-left:17%; margin-top: 1em">procedure printemps(integer deptno)
string ename, dname;
integer empno;
double monthsal;
for
select e.empno, e.ename, d.dname
, e.sal/12 monthsal
from emp e join dept d
on e.deptno = d.deptno
where d.deptno=:deptno
order by e.empno
/
loop
printf "%5d %10s %6.2f %10s0, empno, ename, monthsal, dname;
end loop;
end printemps;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Array for
DML</b></p>
<p style="margin-left:11%; margin-top: 1em">This example
shows how to use the array interface for an insert
statement.</p>
<pre style="margin-left:17%; margin-top: 1em">integer i;
# assume more variables are declared
sql inslin
insert into orderlines
( ordno, linno, product, amount )
values
-- it is fine to use bind by position
-- also when the actual binds are named
( :a, :b, :c, :d)
.
# You do not need to have binds in any
# particular order and
# can mix bind by position and name
bind 3 prodcode, 1 ordno;
bind ":d" amount;
bind 2 i;
# Use array of 10 rows
array 10;
end inslin;
ordno := ... ;
for i:=1 .. linecount loop
# set values
prodcode := ... ; amount := ... ;
inslin; # fill array and flush each 10 rows
end loop;
commit; # will flush array as needed</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Use of
bindout</b></p>
<p style="margin-left:11%; margin-top: 1em">Directly
execute a PL/SQL block having an output bind:</p>
<pre style="margin-left:17%; margin-top: 1em">$bindoutname:on:ret # binds beginning with ’ret’ are bindout
string retdate;
sql execute
BEGIN
:retdate := to_char(sysdate,’DD.MM.YYYY HH24:MI’);
END;
.
end;
printline retdate;</pre>
<p style="margin-left:11%; margin-top: 1em"><b>Dynamic
sql</b></p>
<p style="margin-left:11%; margin-top: 1em">Declare a
dynamic sql statement with an array size and an initial set
of define variables assuming these will always be needed
during actual execution.</p>
<pre style="margin-left:17%; margin-top: 1em">sql myquery
define 1 cola, 2 colb;
array 42;
end;</pre>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">It is a
programming error not to use <b>bindout</b> when needed, and
not doing so can lead to unexpected results. In this
code</p>
<pre style="margin-left:17%; margin-top: 1em">integer val := 10;
begin
:val := 42;
end;
/
printline val, 0+val;</pre>
<p style="margin-left:11%; margin-top: 1em">the output from
the printline statement will be</p>
<p style="margin-left:17%; margin-top: 1em">10 42</p>
<p style="margin-left:11%; margin-top: 1em">since the
string representation of val is set to the string
"10" during the initial assignment, while the
implicit bind will be to the integer representation. The
latter is then used in the expression 0+val. If the correct
bindout had been provided, the string representation
("42") of val would have been generated after
execution of the PL/SQL block.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The scan for
SQL or PL/SQL starts anywhere one of the keywords listed
above are found. If this is used at an inappropriate place,
the rwloadsim parser will report an error.</p>
<p style="margin-left:11%; margin-top: 1em">Previous
versions of rwloadsim did <i>not</i> recognize all SQL
keywords or sets of keywords and you could therefore use
these as identifiers. As SQL scan now is initiated by
<i>any</i> possible keyword, you will get errors if you have
been using these as identifiers and you will need to change
your code.</p>
<p style="margin-left:11%; margin-top: 1em">Note that the
full syntax for <b>create java</b> is more complex than
rwloadsim recognizes and that you therefore will need to
initiate <b>create java</b> by <b>--</b> if it includes any
of the keywords <b>and resolve</b>, <b>and compile</b> or
<b>noforce</b>.</p>
<h2>COPYRIGHT
<a name="COPYRIGHT"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Copyright
© 2023 Oracle Corporation</p>
<p style="margin-left:11%; margin-top: 1em">Licensed under
the Universal Permissive License v 1.0 as shown at
https://oss.oracle.com/licenses/upl</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><a href="sqlexecution.html">sqlexecution(1rwl)</a>,
<a href="sqlstatement.html">sqlstatement(1rwl)</a>, <a href="directive.html">directive(1rwl)</a></p>
<hr>
</body>
</html>