{"slug": "simplified-sql-macros-in-oracle-ai-database-23-26-4", "title": "Simplified SQL Macros in Oracle AI Database 23.26.4", "summary": "Oracle AI Database 23.26.4 introduces Simplified SQL Macros, letting developers define scalar and table macros directly with a new CREATE MACRO statement instead of PL/SQL functions that return SQL strings. The new syntax supports scalar expressions, FROM-clause table macros, and multiple parameters, while invocation of existing macros remains unchanged.", "body_md": "**SQL Macros**, introduced in Oracle Database 21c, allow developers to create reusable SQL expressions and queries. [I previously covered this feature in my article SQL Macros in Oracle Database 21c.](https://dev.to/vahidusefzadeh/sql-macros-in-oracle-database-21c-1igo)\n\nPreviously, creating SQL Macros required PL/SQL functions that returned SQL statements as strings.\n\n**Oracle AI Database 23.26.4** introduces Simplified Macros, allowing developers to define macros directly using the new **CREATE MACRO** statement without writing PL/SQL code.\n\nIn this article, we will explore this enhancement and rewrite some of the examples from [my previous article](https://dev.to/vahidusefzadeh/sql-macros-in-oracle-database-21c-1igo) using the new syntax.\n\nLet’s start with a simple example.\n\nSuppose we have a table named PERSON and want to add 60 to the salary of each employee.\n\nIn earlier releases, we could define a Scalar SQL Macro using a PL/SQL function:\n\n```\nSQL> CREATE OR REPLACE FUNCTION sal_func(\n    sal_param NUMBER\n)\nRETURN VARCHAR2 SQL_MACRO(SCALAR)\nIS\nBEGIN\n    RETURN q'{\n        sal_param + 60\n    }';\nEND;\n/\nFunction created\n```\n\nWe could then use this function in a SQL statement:\n\n```\nSQL> select sum(sal) from(select salary+60 as sal from person);\n  SUM(SAL)\n----------\n       197\n```\n\nWith Oracle AI Database 23.26.4, we can define the same macro directly using SQL:\n\n```\nSQL> drop FUNCTION sal_func;\nFunction dropped\nSQL> CREATE MACRO sal_func(sal_param NUMBER) AS\n        sal_param + 60;\nDone\n```\n\nThe invocation remains unchanged:\n\n```\nSQL> select sum(sal) from(select salary+60 as sal from person);\n  SUM(SAL)\n----------\n       197\n```\n\nAs you can see, we no longer need to create a PL/SQL function, specify SQL_MACRO(SCALAR), or return the SQL expression as a character string.\n\nSQL Macros can also return SQL queries and be used in the FROM clause.\n\nConsider the following example from my previous article. This macro returns the size, in MB, of the largest segment belonging to a specified schema.\n\nIn earlier releases, we could implement it as follows:\n\n```\nSQL>CREATE OR REPLACE FUNCTION table_macro_Mesal(\n    p_tab VARCHAR2\n)\nRETURN VARCHAR2 SQL_MACRO(TABLE)\nIS\nBEGIN\n    RETURN q'{\n        SELECT MAX(bytes/1024/1024) SIZE_MB\n        FROM dba_segments d\n        WHERE owner = table_macro_Mesal.p_tab\n    }';\nEND;\n/\nFunction created\nSQL> SELECT * FROM table_macro_Mesal('SYS');\n   SIZE_MB\n----------\n       168\n```\n\nWith the new syntax, the definition becomes much simpler:\n\n```\nSQL>CREATE MACRO table_macro_Mesal(p_tab VARCHAR2) AS\n    SELECT MAX(bytes/1024/1024) SIZE_MB\n    FROM dba_segments d\n    WHERE owner = table_macro_Mesal.p_tab;\nDone\n```\n\nWe can invoke the macro in exactly the same way:\n\n```\nSQL> SELECT * FROM table_macro_Mesal('SYS');\n   SIZE_MB\n----------\n       168\n```\n\nThe macro accepts a schema name as its parameter and returns a query that calculates the maximum segment size for that schema.\n\nThe new syntax also supports multiple parameters.\n\nLet’s consider another example from my previous article. This macro accepts two parameters:\n\n• **cnt**: The number of segments to return.\n\n• **typ**: The segment type used to filter the results.\n\nThe original function-based implementation was:\n\n```\nSQL> CREATE OR REPLACE FUNCTION table_macro_Mesal2(\n    cnt NUMBER,\n    typ VARCHAR2\n)\nRETURN VARCHAR2 SQL_MACRO(TABLE)\nIS\nBEGIN\n    RETURN q'{\n        SELECT owner,\n               segment_name,\n               bytes / 1024 / 1024 SIZE_MB\n        FROM dba_segments\n        WHERE segment_type = table_macro_Mesal2.typ\n        ORDER BY bytes DESC\n        FETCH FIRST cnt ROWS ONLY\n    }';\nEND;\n/\nFunction created\nSQL> SELECT * FROM table_macro_Mesal2(5, 'TABLE');\nOWNER  SEGMENT_NAME            SIZE_MB\n------ -------------------- ----------\nSYS    IDL_UB2$                     18\nSYS    IDL_UB1$                     15\nSYS    OBJ$                         11\nMDSYS  SDO_CS_SRS                    9\nSYS    DEPENDENCY$                   6\n```\n\nUsing the new CREATE MACRO statement, we can define it as follows:\n\n```\nSQL> CREATE MACRO table_macro_Mesal2(\n    cnt NUMBER,\n    typ VARCHAR2\n) AS\n    SELECT owner,\n           segment_name,\n           bytes / 1024 / 1024 SIZE_MB\n    FROM dba_segments\n    WHERE segment_type = table_macro_Mesal2.typ\n    ORDER BY bytes DESC\n    FETCH FIRST table_macro_Mesal2.cnt ROWS ONLY;\nDone\nSQL> SELECT * FROM table_macro_Mesal2(5, 'TABLE');\nOWNER  SEGMENT_NAME            SIZE_MB\n------ -------------------- ----------\nSYS    IDL_UB2$                     18\nSYS    IDL_UB1$                     15\nSYS    OBJ$                         11\nMDSYS  SDO_CS_SRS                    9\nSYS    DEPENDENCY$                   6\n```\n\nOracle AI Database 23.26.4 introduces additional capabilities for native SQL Macros, including default parameter values, table parameters, reusable predicates, and binding macros to specific tables or views.\n\nOracle also provides new data dictionary views, such as USER_MACROS and USER_MACRO_PARAMETERS, to inspect macro definitions and their parameters.\n\n```\nSQL> select owner,name,param_count from USER_MACROS;\nOWNER  NAME                  PARAM_COUNT\n------ --------------------  -----------\nVAHID  SAL_FUNC                        1\nVAHID  TABLE_MACRO_MESAL               1\nVAHID  TABLE_MACRO_MESAL2              2\n```\n\nI will explore these capabilities in more detail in future articles.", "url": "https://wpnews.pro/news/simplified-sql-macros-in-oracle-ai-database-23-26-4", "canonical_source": "https://dev.to/vahidusefzadeh/simplified-sql-macros-in-oracle-ai-database-23264-3npi", "published_at": "2026-11-09 07:51:00+00:00", "updated_at": "2026-11-09 07:52:48.360979+00:00", "lang": "en", "topics": ["ai-products", "developer-tools"], "entities": ["Oracle", "Oracle AI Database", "PL/SQL"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/simplified-sql-macros-in-oracle-ai-database-23-26-4", "markdown": "https://wpnews.pro/news/simplified-sql-macros-in-oracle-ai-database-23-26-4.md", "text": "https://wpnews.pro/news/simplified-sql-macros-in-oracle-ai-database-23-26-4.txt", "jsonld": "https://wpnews.pro/news/simplified-sql-macros-in-oracle-ai-database-23-26-4.jsonld"}}