Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:54:24+00:00 2026-06-09T14:54:24+00:00

How to raise error from PostgreSQL SQL statement if some condition is met? I

  • 0

How to raise error from PostgreSQL SQL statement if some condition is met?
I tried code below but got error.

CREATE OR REPLACE FUNCTION "exec"(text)
  RETURNS text AS
$BODY$ 
    BEGIN 
      EXECUTE $1; 
      RETURN $1; 
    END; 
$BODY$
  LANGUAGE plpgsql VOLATILE;

-- ERROR:  syntax error at or near "raise"
-- LINE 1: raise 'test' 

SELECT exec('raise ''test'' ') WHERE TRUE

In real application TRUE is replaced by some condition.

Update

I tried to extend answer to pass exception message parameters.
Tried code below but got syntax error.
How to pass message parameters ?

CREATE OR REPLACE FUNCTION exec(text, variadic ) 
  RETURNS void LANGUAGE plpgsql AS 
$BODY$  
BEGIN  
   RAISE EXCEPTION  $1, $2;  
END;  
$BODY$; 

SELECT exec('Exception Param1=% Param2=%', 'param1', 2 ); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:54:25+00:00Added an answer on June 9, 2026 at 2:54 pm

    You cannot call RAISE dynamically (with EXECUTE) in PL/pgSQL – that only works for SQL statements, and RAISE is a PL/pgSQL command.

    Use this simple function instead:

    CREATE OR REPLACE FUNCTION f_raise(text)
      RETURNS void
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       RAISE EXCEPTION '%', $1;
    END
    $func$;
    

    Call:

    SELECT f_raise('My message is empty!');
    

    Related:

    • Generate an exception with a Context

    Additional answer to comment

    CREATE OR REPLACE FUNCTION f_raise1(VARIADIC text[])
      RETURNS void
      LANGUAGE plpgsql AS
    $func$
    BEGIN 
       RAISE EXCEPTION 'Reading % % %!', $1[1], $1[2], $1[3];
    END
    $func$;
    

    Call:

    SELECT f_raise1('the','manual','educates');
    
    • VARIADIC is not a data type, but an argument mode.

    • Elements have to be handled like any other array element.

    • To use multiple variables in a RAISE statement, put multiple % into the message text.

    The above example will fail if no $3 is passed. You’d have to assemble a string from the variable number of input elements. Example:

    CREATE OR REPLACE FUNCTION f_raise2(VARIADIC _arr text[]) 
      RETURNS void
      LANGUAGE plpgsql AS 
    $func$  
    DECLARE
       _msg text := array_to_string(_arr, ' and ');  -- simple string construction
    BEGIN  
       RAISE EXCEPTION 'Reading %!', _msg;
    END
    $func$;
    

    Call:

    SELECT f_raise2('the','manual','educates');
    

    I doubt you need a VARIADIC parameter for this at all. Read the manual here.
    Instead, define all parameters, maybe add defaults:

    CREATE OR REPLACE FUNCTION f_raise3(_param1 text = ''
                                      , _param2 text = ''
                                      , _param3 text = 'educates')
      RETURNS void
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       RAISE EXCEPTION 'Reading % % %!', $1, $2, $3;
    END 
    $func$;
    

    Call:

    SELECT f_raise3('the','manual','educates');
    

    Or:

    SELECT f_raise3();  -- defaults kick in
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I raise an error in an AFTER UPDATE trigger in Sql Server 2005,
Why does the following query raise the error below for a row with a
Sometimes postgresql raise error deadlocks. In trigger for table setted FOR UPDATE. Table comment:
Question: I want to test an if statement in PostgreSQL: IF (SELECT COUNT(*) FROM
from : 5/19/2011 to : 2011-05-19 I need it to raise an error when
I need to rescue a Timeout::Error raised from a the Redis library but i'm
I got strange error message when tried to save first_name, last_name to Django's auth_user
I have a SQL Statement that works as I want. select COUNT(*), MIN(emailed_to)from email.email_archive
What's the proper syntax here? If (@timestamp < (Select PromoStartTimestamp From @promo)) RAISERROR('Code not
I'm just wondering where this syntax documented: 1 > 2 || raise(error) I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.