Search code examples
sqloracle-databasequoting

Escaping end of quotation Oracle SQL


In my stored procedures, I'd normally go like execute immediate q'[code to execute]. However, I have something in my code which has '$[*]'. The quote and bracket is interpreted by Oracle as the end of my quote. How can I escape this? Please find below full code.

    EXECUTE IMMEDIATE q'[
CREATE OR REPLACE VIEW vw_err_text AS
    WITH aux AS (
        SELECT
            err_txt
        FROM
            u339990_mr2
        WHERE
            err_txt IS NOT NULL
    )
    SELECT
        error_text,
        COUNT(*) AS ct,
        round(RATIO_TO_REPORT(COUNT(1)) OVER() * 100, 2) perc
    FROM
        aux CROSS APPLY
            JSON_TABLE(err_txt, '$[*]'
                COLUMNS
                    error_text PATH '$'
            )
    GROUP BY
        error_text
    ORDER BY
        ct DESC,
        error_text
        ]'
    ;

Solution

  • Use a different character (or characters) for the q quotation syntax.

    q'[ ... ]' is correct, but you can also use q'( ... )' (or the same with curly braces or < and >). Also, you can use any single character (not paired) as in q'@ ... @' or q'^ ... ^' - just use a character where the closing combination doesn't naturally arise in your data. This is exactly why the syntax allows this flexibility.

    https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#i42617