Skip to content

Instantly share code, notes, and snippets.

View loiclefevre's full-sized avatar
🏠
Working from home

Loïc LEFEVRE loiclefevre

🏠
Working from home
View GitHub Profile
@loiclefevre
loiclefevre / json_table.sql
Last active April 13, 2022 13:37
JSON_TABLE simplification
CREATE TABLE j_order (
id VARCHAR2(255) DEFAULT SYS_GUID()
NOT NULL PRIMARY KEY,
purchase_order JSON
);
INSERT INTO j_order (purchase_order)
VALUES ('{"PONumber": 1600, "Requestor": "Alexis Bull",
"Items": [ {"Description": "One Magic Christmas",
"UnitPrice": 9.95, "Quantity": 1.0},
@loiclefevre
loiclefevre / listagg.sql
Created April 8, 2022 10:31
Listagg function
-- Getting list of columns per table
SELECT table_name,
LISTAGG(column_name, ', '
ON OVERFLOW TRUNCATE
'... with more column(s) '
WITH COUNT
) WITHIN GROUP
(ORDER BY column_id) AS columns
FROM all_tab_columns
@loiclefevre
loiclefevre / sql_table_macro.sql
Last active April 12, 2022 19:50
SQL Table Macro (19.7+)
CREATE TABLE planets (
json_document BLOB,
CONSTRAINT json_document_is_json CHECK (json_document IS JSON)
);
INSERT INTO planets (json_document)
VALUES ( '[ {"name":"Mercury"}, {"name":"Venus"}, {"name":"Earth"},
{"name":"Mars"}, {"name":"Jupiter"}, {"name":"Saturn"},
{"name":"Uranus"}, {"name":"Neptune"} ]' );
@loiclefevre
loiclefevre / long_identifiers.sql
Created April 13, 2022 08:04
Long identifiers since 12.2 release (2017)
-- Supporting 128 bytes long identifiers since 2017
CREATE TABLE my_table_which_really_deserves_such_a_long_name (
unique_identifier_that_explains_it_all_very_easily NUMBER,
-- Also supporting 32767 bytes for VARCHAR2 since 2013
message VARCHAR2(32767)
);
INSERT INTO my_table_which_really_deserves_such_a_long_name
VALUES ( 1, 'Hello World!' );
@loiclefevre
loiclefevre / oracle_machine_learning_for_sql.sql
Created April 14, 2022 20:30
Oracle Machine Learning for SQL
CREATE TABLE customers (
id NUMBER PRIMARY KEY,
children NUMBER,
age NUMBER,
gender CHAR(1),
salary NUMBER,
married CHAR(1),
will_buy_an_house NUMBER
);
@loiclefevre
loiclefevre / recover_dropped_table.sql
Created April 20, 2022 07:35
Recyclebin and recovering dropped table
CREATE TABLE very_important_data (
salary_wanted NUMBER CHECK (salary_wanted > 1000000)
);
INSERT INTO very_important_data (salary_wanted) VALUES( 2000000 );
COMMIT;
DROP TABLE very_important_data;
@loiclefevre
loiclefevre / json_object.sql
Created April 21, 2022 09:03
JSON_OBJECT()
CREATE TABLE doctors ( first_name VARCHAR2(100),
last_name VARCHAR2(100) );
INSERT INTO doctors VALUES ( 'John', 'Doe' );
INSERT INTO doctors VALUES ( 'Foo', 'Bar' );
COMMIT;
@loiclefevre
loiclefevre / enable_graalvm_native_compilation_linux_8.sh
Created April 24, 2022 09:21
Enable GraalVM native compilation on OCI VM with Oracle Linux 8
sudo yum-config-manager --enable ol8_codeready_builder
sudo yum install -y libstdc++-static
sudo yum install -y gcc glibc-devel zlib-devel
@loiclefevre
loiclefevre / get_event_data.sql
Last active June 26, 2024 13:12
Function to query TEQ JSON payloads in SQL
create or replace function get_event_data(p_raw in raw, p_blob in blob) return clob
as
key_len PLS_INTEGER;
value_offset PLS_INTEGER;
value_len PLS_INTEGER;
value_len_offset PLS_INTEGER;
value_raw raw(2000);
temp_raw raw(8);
len_size PLS_INTEGER := 4;
begin_offset PLS_INTEGER := 1;