Home > Software design >  In pl/pgsql, is there a way to RAISE calculations?
In pl/pgsql, is there a way to RAISE calculations?

Time:05-18

I'm trying to test out some code, and it would be great if there was a way to RAISE NOTICE like i would with print() or console.log().

here is my attempt, but not sure how this works:

DO
$cols$
DECLARE
    qty_cols INT := 3;
    current_month INT := ( SELECT EXTRACT(MONTH FROM DATE(NOW())) );
    month_col INT;
BEGIN
    FOR month_col IN 1..qty_cols LOOP
        IF current_month < (month_col 1) THEN
            --RAISE NOTICE (12 current_month) - month_col;
            RAISE NOTICE '%', (12 current_month) - month_col;
        ELSE
            --RAISE NOTICE (current_month - month_col);
            RAISE NOTICE '%', (12 current_month) - month_col;
        END IF;
    END LOOP;
END
$cols$;

i know that i can use % symbols to replace variables, but this doesn't seem like just substituting into a variable...

CodePudding user response:

RAISE NOTICE expects a following argument that is a format string with % placeholders, into which the subsequent arguments will be placed. The subsequent arguments can be calculations

After level if any, you can write a format (which must be a simple string literal, not an expression). The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message. Inside the format string, % is replaced by the string representation of the next optional argument's value. Write %% to emit a literal %. The number of arguments must match the number of % placeholders in the format string, or an error is raised during the compilation of the function.

Minimally a format string for a single subsequent argument would look like:

RAISE NOTICE '%', (12 current_month) - month_col;

Though the intent is perhaps to be more descriptive:

RAISE NOTICE 'The month difference is %', (12 current_month) - month_col;
  • Related