-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsqlplus_bind_variables.sql
More file actions
64 lines (48 loc) · 1.53 KB
/
Copy pathsqlplus_bind_variables.sql
File metadata and controls
64 lines (48 loc) · 1.53 KB
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
SQL*Plus bind variables
SQL*Plus bind variables can be declared outwith PL/SQL blocks.
I find this useful if I want to put values into a SQL statement which uses bind variables and I don't want the optimizer to change the execution plan;
(it will almost certainly do so if you transpose the bind variables with literal values.)
SQL> var gr1 number;
SQL> var gr2 varchar2(20);
SQL> exec :gr1 := 7369;
SQL> exec :gr2 := 'GARRY';
SQL> print
GR1
----------
7369
GR2
--------------------------------
GARRY
SQL> select :gr1, :gr2 from dual;
:GR1 :GR2
---------- --------------------------------
7369 GARRY
SQL> select * from scott.emp where empno = :gr1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH C
But this wont allow you passing a date type value.
Another way is to use the pl/sql
SQL>
SQL> declare
2 v_dts timestamp(3) := systimestamp;
3 v_count integer;
4 begin
5 select count(*) as example2
6 into v_count
7 from x
8 where created_dts = v_dts;
9 end;
10 /
SQL> declare
2 v_date date := sysdate;
3 v_count integer;
4 begin
5 select count(*) as example1
6 into v_count
7 from x
8 where created_date = v_date;
9 end;
10 /
To view bind information:
select name, position, datatype_string, was_captured, value_string,anydata.accesstimestamp(value_anydata) from v$sql_bind_capture where sql_id in ('2avwvgg1qp025','936pz56dqmpdc', '9kvty17uujyva') ;