Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 24, 2016
0 parents commit 93975cf
Show file tree
Hide file tree
Showing 225 changed files with 6,942 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_01.txt
@@ -0,0 +1,9 @@
PROCEDURE insert_blob_proc IS
Blob_loc BLOB;
BEGIN
SELECT ad_photo INTO Blob_loc
FROM Print_media
WHERE product_id = 3106 AND ad_id=13001;
INSERT INTO Print_media VALUES (2056, 12001, Blob_loc);
END;

31 changes: 31 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_02.txt
@@ -0,0 +1,31 @@
#include <oci.h>
#include <stdio.h>
#include <sqlca.h>
void Sample_Error()
{
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf(�%.*s\n�, sqlca.sqlerrm.sqlerrml, sqlca.sqlerrm.sqlerrmc);
EXEC SQL ROLLBACK WORK RELEASE;
exit(1);
}
void insertBLOB_proc()
{
OCIBlobLocator *Lob_loc;
EXEC SQL WHENEVER SQLERROR DO Sample_Error();
/* Initialize the BLOB Locator: */
EXEC SQL ALLOCATE :Lob_loc;
EXEC SQL SELECT ad_photo INTO :Lob_loc
FROM Print_media WHERE product_id = 2268 AND ad_id = 21001;
/* Insert into the row where product_id = 3106 and ad_id = 13001: */
EXEC SQL INSERT INTO Print_media
VALUES (3106, 13001, :Lob_loc);
/* Release resources held by the locator: */
EXEC SQL FREE :Lob_loc;
}
void main()
{
char *samp = �pm/pm�;
EXEC SQL CONNECT :pm;
insertBLOB_proc();
EXEC SQL ROLLBACK WORK RELEASE;
}
6 changes: 6 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_03.txt
@@ -0,0 +1,6 @@
create table customers (
pin_number number(6) );

alter table customers
add constraint customers_pk
primary key (pin_number);
17 changes: 17 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_04.txt
@@ -0,0 +1,17 @@
create or replace
procedure gen_customer is
v_new_cid customers.pin_number%type;
begin
loop
begin
v_new_cid := round(dbms_random.value(1000000,9999999));
insert into customers
values (v_new_cid);
exit;
exception when dup_val_on_index then
null;
end;
end loop;
end;
/

15 changes: 15 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_05.txt
@@ -0,0 +1,15 @@
set timing on
begin
gen_customer;
commit;
end;
/

begin
for i in 1 .. 100000 loop
gen_customer;
end loop;
commit;
end;
/

15 changes: 15 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_06.txt
@@ -0,0 +1,15 @@
alter table CUSTOMERS modify PIN_NUMBER number(12);
create sequence cust_seq cache 1000
start with 100000;

create or replace
procedure gen_customer is
v_new_cid customers.pin_number%type;
begin
insert into customers
values (cust_seq.nextval*100000+
round(dbms_random.value(100000,999999)));
end;
/


7 changes: 7 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_07.txt
@@ -0,0 +1,7 @@
begin
for i in 1 .. 10000 loop
gen_customer;
end loop;
commit;
end;
/
21 changes: 21 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_08.txt
@@ -0,0 +1,21 @@
alter system set job_queue_processes = 20;
create table CUSTLOG ( elapsed_centiseconds number);

declare
j number;
job_string varchar2(1000) :=
�declare
s number := dbms_utility.get_time;
begin
for i in 1 .. 10000 loop
gen_customer;
end loop;
insert into custlog values (dbms_utility.get_time-s);
commit;
end;�;
begin
for i in 1 .. 20 loop
dbms_job.submit(j,job_string);
end loop;
end;
/
29 changes: 29 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_09.txt
@@ -0,0 +1,29 @@
create table people( pid primary key )
organization index
as select rownum from all_objects
where rownum <= 10000;

create or replace
procedure literals is
c number;
p number;
x number;
x1 number;
begin
for i in 1 .. 10000 loop
c := dbms_sql.open_cursor;
dbms_sql.parse(c,
�select pid from people �||
�where pid = �||i, dbms_sql.native);
x := dbms_sql.execute(c);
x1 := dbms_sql.fetch_rows(c);
dbms_sql.close_cursor(c);
end loop;
end;
/

set timing on
alter session set sql_trace = true;
exec literals;
alter session set sql_trace = false;

29 changes: 29 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_10.txt
@@ -0,0 +1,29 @@
create or replace
procedure literals(tag number) is
c number;
p number;
x number;
x1 number;
begin
for i in 1 .. 10000 loop
c := dbms_sql.open_cursor;
dbms_sql.parse(c,
�select pid t�||tag||� from people �||
�where pid = �||i, dbms_sql.native);
x := dbms_sql.execute(c);
x1 := dbms_sql.fetch_rows(c);
dbms_sql.close_cursor(c);
end loop;
end;
/


exec literals(1);
exec literals(2);
exec literals(3);
exec literals(4);

select sid, event, time_waited
from v$session_event
where sid = &sid
and event = �latch free�;
22 changes: 22 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_11.txt
@@ -0,0 +1,22 @@
create or replace
procedure binding is
c number;
p number;
x number;
x1 number;
begin
for i in 1 .. 10000 loop
c := dbms_sql.open_cursor;
dbms_sql.parse(c,
�select pid from people �||
�where pid = :b1�, dbms_sql.native);
dbms_sql.bind_variable(c,�:b1�,i);
x := dbms_sql.execute(c);
x1 := dbms_sql.fetch_rows(c);
dbms_sql.close_cursor(c);
end loop;
end;
/

alter session set sql_trace = true;
exec binding
10 changes: 10 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_12.txt
@@ -0,0 +1,10 @@
create or replace
view V$MYSTATS as
select s.name, m.value
from v$mystat m, v$statname s
where s.statistic# = m.statistic#;

grant select on V$MYSTATS to public;
create or replace public synonym V$MYSTATS for V$MYSTATS;
select * from v$mystats
where name like �parse%�;
19 changes: 19 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_13.txt
@@ -0,0 +1,19 @@
create or replace
procedure binding is
c number;
p number;
x number;
x1 number;
begin
c := dbms_sql.open_cursor;
dbms_sql.parse(c,
�select pid from people �||
�where pid = :b1�, dbms_sql.native);
for i in 1 .. 10000 loop
dbms_sql.bind_variable(c,�:b1�,i);
x := dbms_sql.execute(c);
x1 := dbms_sql.fetch_rows(c);
end loop;
dbms_sql.close_cursor(c);
end;
/
11 changes: 11 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_14.txt
@@ -0,0 +1,11 @@
create or replace
procedure EASY_AS_THAT is
x1 number;
begin
for i in 1 .. 10000 loop
select pid into x1
from people
where pid = i;
end loop;
end;
/
10 changes: 10 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_15.txt
@@ -0,0 +1,10 @@
create or replace
procedure UPDATE_EMP(p_empno number, p_decrease number) is
begin
update EMP
set SAL = SAL / p_decrease
where empno = p_empno;
end;
/
exec UPDATE_EMP(7369,2);
exec UPDATE_EMP(7369,0);
14 changes: 14 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_16.txt
@@ -0,0 +1,14 @@
create or replace
procedure UPDATE_EMP(p_empno number, p_decrease number,
p_success out boolean) is
begin
if p_decrease = 0 then
p_success := false;
else
update EMP
set SAL = SAL / p_decrease
where empno = p_empno;
p_success := true;
end if;
end;
/
39 changes: 39 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_17.txt
@@ -0,0 +1,39 @@
create or replace
function binary_and(x number, y number) return number is
max_bin number(22) := power(2,64);
l_x number := x;
l_y number := y;
result number := 0;
begin
for i in reverse 0 .. 64 loop
if l_x >= max_bin and l_y >= max_bin then
result := result + max_bin;
end if;
if l_x >= max_bin then
l_x := l_x - max_bin;
end if;
if l_y >= max_bin then
l_y := l_y - max_bin;
end if;
max_bin := max_bin/2;
end loop;
return result;
end;
/

declare
x number;
begin
for i in 1 .. 50000 loop
x:= binary_and(i,i+1);
end loop;
end;
/
declare
x number;
begin
for i in 1 .. 50000 loop
x:= bitand(i,i+1);
end loop;
end;
/
17 changes: 17 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_18.txt
@@ -0,0 +1,17 @@
create table SRC ( x number ) pctfree 0;
insert into SRC
select rownum
from all_objects
where rownum < 10000;


create table T1 as
select trunc(created) created
from all_objects;

select trunc(sysdate)-14+x created, count(created) no_of_obj
from t1, src
where trunc(sysdate)-14+x = t1.created(+)
and x <= 14
group by trunc(sysdate)-14+x
/
25 changes: 25 additions & 0 deletions 2174_MasteringPLSQL_CODE/Ch01/ch1_19.txt
@@ -0,0 +1,25 @@
create or replace
type date_list is table of Date;
/
create or replace
function pipe_date(p_start date, p_limit number)
return date_list pipelined is
begin
for i in 0 .. p_limit-1 loop
pipe row (p_start + i);
end loop;
return;
end;
/

select column_value, count(created) no_of_obj
from t1, table(pipe_date(trunc(sysdate)-14,14))
where column_value = t1.created(+)
group by column_value
/

select /*+ CARDINALITY(t 14) */ column_value, count(created) no_of_obj
from t1, table(pipe_date(trunc(sysdate)-14,14)) t
where column_value = t1.created(+)
group by column_value
/

0 comments on commit 93975cf

Please sign in to comment.