An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 12, 2022 10:32 am GMT
Original Link: https://dev.to/loiclefevre/reusable-sql-templates-learn-about-sql-macros-3527
Reusable SQL templates: learn about SQL Macros
SQL Table Macro: create reusable SQL templates (FROM clause) you can pass tables, and other parameters to at runtime.
Documentation: https://docs.oracle.com/en/database/oracle/oracle-database/19/newft/sql-macros-sqm.html
SQL Table Macro is a 21c feature backported to the 19.7 release update.
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"} ]' );COMMIT;-- One row retrieved containing a JSON arraySELECT JSON_SERIALIZE(json_document) AS array FROM planets; ARRAY-------------------------------------------------------------[ {"name":"Mercury"}, {"name":"Venus"}, {"name":"Earth"}, ]CREATE FUNCTION unwind(jsonTable DBMS_TF.TABLE_T) RETURN VARCHAR2 SQL_MACRO ISBEGIN RETURN q'{SELECT d.array_item AS JSON_DOCUMENT, d.order_id FROM unwind.jsonTable NESTED json_document COLUMNS ( NESTED PATH '$[*]' COLUMNS ( array_item CLOB FORMAT JSON PATH '$', order_id FOR ORDINALITY ) ) d }';END;/SELECT * FROM unwind(planets); JSON_DOCUMENT ORDER_ID------------------ ----------{"name":"Mercury"} 1 {"name":"Venus"} 2 {"name":"Earth"} 3 {"name":"Mars"} 4{"name":"Jupiter"} 5 {"name":"Saturn"} 6 {"name":"Uranus"} 7{"name":"Neptune"} 8
Remark for Pluto: according to NASA, this is no more considered as the 9th planet
Kudos to Chris Saxon for helping a lot finding the summary for such a great feature!
Original Link: https://dev.to/loiclefevre/reusable-sql-templates-learn-about-sql-macros-3527
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To