forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sql
More file actions
159 lines (140 loc) · 4.71 KB
/
Copy pathinit.sql
File metadata and controls
159 lines (140 loc) · 4.71 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
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
-- Table used for Generation of XML and JSON from Releation table
create table orders(
orderId number generated by default on null as identity primary key,
productId number,
order_count number,
order_date date,
order_invoice_id number
);
insert into orders(productId, order_count, order_date, order_invoice_id)
values (3, 1, to_date('11/29/2022', 'MM/DD/YYYY'), 219);
insert into orders(productId, order_count, order_date, order_invoice_id)
values (2, 2, to_date('11/29/2022', 'MM/DD/YYYY'), 219);
insert into orders(productId, order_count, order_date, order_invoice_id)
values (4, 2, sysdate, 218);
insert into orders(productId, order_count, order_date, order_invoice_id)
values (2, 5, sysdate, 218);
insert into orders(productId, order_count, order_date, order_invoice_id)
values (3, 1, sysdate, 218);
-- JSON column example
CREATE TABLE PRODUCTS(
productId NUMBER generated by default on null as identity primary key,
productInformation json
);
insert into products(productInformation)
values ('{
"Title": "Gone the Days of Summer",
"ProductType": "Book",
"Author": "Eliza Smith",
"Stock": 5
}');
insert into products(productInformation)
values ('{
"Title": "At the End of the Universe and Spring",
"ProductType": "Book",
"Author": "Frank Thorn",
"Stock": 2
}');
insert into products(productInformation)
values ('{
"Title": "As Calm as Winter Nights",
"ProductType": "Book",
"Stock": 0
}');
-- Tables referenced in the blog
create table PROFILES_19c (
profileId number generated by default on null as identity primary key,
username varchar2(32) unique,
preferences VARCHAR2(4000),
settings BLOB,
CONSTRAINT ensure_pref_json_a CHECK (preferences IS JSON),
CONSTRAINT ensure_sett_json_a CHECK (settings IS JSON)
);
create table PROFILES (
profileId number generated by default on null as identity primary key,
username varchar2(32),
preferences JSON,
settings JSON
);
-- XMLTYPE as a table example
CREATE TABLE VENDOR_INVOICES of XMLTYPE;
insert into VENDOR_INVOICES values(XMLTYPE('<?xml version="1.0"?>
<Invoice><InvoiceId>273</InvoiceId></Invoice>'));
-- XMLTYPE as a column example
CREATE TABLE INVOICES(
invoiceId NUMBER generated by default on null as identity primary key,
invoice xmltype
);
insert into invoices(invoice) values (XMLTYPE('<?xml version="1.0"?>
<Invoice>
<InvoiceId>272</InvoiceId>
<ShippingInformation>
<ContactInformation>
<FirstName>Jane</FirstName>
<LastName>Smith</LastName>
<Phone>7408812361</Phone>
</ContactInformation>
<ShippingAddress>
<Address>33079 23 Mile Rd</Address>
<City>Chesterfield</City>
<State>Michigan</State>
<Zip>48047</Zip>
<Country>United States</Country>
</ShippingAddress>
</ShippingInformation>
<Orders>
<Order>
<ProductId>3</ProductId>
<Count>5</Count>
</Order>
</Orders>
</Invoice>
'));
-- sys_refcursor example with a function
create or replace function get_information return sys_refcursor is
v_refc sys_refcursor;
begin
open v_refc for
select p.productInformation from products p;
return v_refc;
end;
/
-- REF Cursor Package
CREATE OR REPLACE PACKAGE products_data AUTHID DEFINER AS
TYPE prdcursorA IS REF CURSOR RETURN products%ROWTYPE;
TYPE prdcursorB IS REF CURSOR;
PROCEDURE open_prod_cv_a(products_curvar IN OUT prdcursorA);
PROCEDURE open_prod_cv_b(products_curvar IN OUT prdcursorB, source VARCHAR2);
END products_data;
/
CREATE or REPLACE PACKAGE BODY products_data AS
--- strong cusror
PROCEDURE open_prod_cv_a(products_curvar IN OUT prdcursorA) IS
BEGIN
OPEN products_curvar FOR SELECT * from products;
END open_prod_cv_a;
--- weak cursor (different return types)
PROCEDURE open_prod_cv_b(products_curvar IN OUT prdcursorB, source VARCHAR2) IS
BEGIN
CASE source
WHEN 'Products' then OPEN products_curvar FOR SELECT * FROM PRODUCTS;
WHEN 'Invoices' then OPEN products_curvar FOR SELECT * FROM INVOICES;
WHEN 'Orders' then OPEN products_curvar FOR SELECT * FROM ORDERS;
END CASE;
END open_prod_cv_b;
END products_data;
/
-- Associative Array procedure
create or replace procedure get_order_status(status_id in number, return_status out varchar2) is
TYPE status IS TABLE OF VARCHAR2(250)
INDEX BY VARCHAR2(64);
orderStatus status;
begin
orderStatus(1) := 'Received';
orderStatus(2) := 'Processing';
orderStatus(3) := 'Shipped';
orderStatus(4) := 'Completed';
orderStatus(5) := 'Cancelled';
return_status := orderStatus(status_id);
end;
/