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

  • SEARCH
  • Home
  • 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 6247793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:56:22+00:00 2026-05-24T12:56:22+00:00

I am new to PostgreSQL. Could anybody please correct this query. BEGIN TRANSACTION; BEGIN;

  • 0

I am new to PostgreSQL. Could anybody please correct this query.

BEGIN TRANSACTION;

BEGIN;
    CREATE TABLE "Logs"."Events"
    (
        EventId BIGSERIAL NOT NULL PRIMARY KEY,
        PrimaryKeyId bigint NOT NULL,
        EventDateTime date NOT NULL DEFAULT(now()),
        Action varchar(12) NOT NULL,
        UserId integer NOT NULL REFERENCES "Office"."Users"(UserId),
        PrincipalUserId varchar(50) NOT NULL DEFAULT(user)
    );

    CREATE TABLE "Logs"."EventDetails"
    (
        EventDetailId BIGSERIAL NOT NULL PRIMARY KEY,
        EventId bigint NOT NULL REFERENCES "Logs"."Events"(EventId),
        Resource varchar(64) NOT NULL,
        OldVal varchar(4000) NOT NULL,
        NewVal varchar(4000) NOT NULL
    );


    COMMIT TRANSACTION;
    RAISE NOTICE 'Task completed sucessfully.'
EXCEPTION;
    ROLLBACK TRANSACTION;
    RAISE ERROR @ErrorMessage, @LineNumber, @ErrorState --how to catch errors and display them????
END;

Questions:

  1. How to print a message like ‘PRINT’ in T-SQL?
  2. How to raise errors with exception information?
  • 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-05-24T12:56:22+00:00Added an answer on May 24, 2026 at 12:56 pm

    To catch the error message and its code:

    do $$       
    begin
    
        create table yyy(a int);
        create table yyy(a int); -- this will cause an error
    
    exception when others then 
    
        raise notice 'The transaction is in an uncommittable state. '
                     'Transaction was rolled back';
    
        raise notice '% %', SQLERRM, SQLSTATE;
    
    end; $$ 
    language 'plpgsql';
    

    Haven’t found the line number yet

    UPDATE April, 16, 2019

    As suggested by Diego Scaravaggi, for Postgres 9.2 and up, use GET STACKED DIAGNOSTICS:

    do language plpgsql $$
    declare
        v_state   TEXT;
        v_msg     TEXT;
        v_detail  TEXT;
        v_hint    TEXT;
        v_context TEXT;
    begin
    
        create table yyy(a int);
        create table yyy(a int); -- this will cause an error
    
    exception when others then 
    
        get stacked diagnostics
            v_state   = returned_sqlstate,
            v_msg     = message_text,
            v_detail  = pg_exception_detail,
            v_hint    = pg_exception_hint,
            v_context = pg_exception_context;
    
        raise notice E'Got exception:
            state  : %
            message: %
            detail : %
            hint   : %
            context: %', v_state, v_msg, v_detail, v_hint, v_context;
    
        raise notice E'Got exception:
            SQLSTATE: % 
            SQLERRM: %', SQLSTATE, SQLERRM;     
    
        raise notice '%', message_text; -- invalid. message_text is contextual to GET STACKED DIAGNOSTICS only
    
    end; $$;
    

    Result:

    NOTICE:  Got exception:
            state  : 42P07
            message: relation "yyy" already exists
            detail : 
            hint   : 
            context: SQL statement "create table yyy(a int)"
    PL/pgSQL function inline_code_block line 11 at SQL statement
    NOTICE:  Got exception:
            SQLSTATE: 42P07 
            SQLERRM: relation "yyy" already exists
    
    ERROR:  column "message_text" does not exist
    LINE 1: SELECT message_text
                   ^
    QUERY:  SELECT message_text
    CONTEXT:  PL/pgSQL function inline_code_block line 33 at RAISE
    SQL state: 42703
    

    Aside from GET STACKED DIAGNOSTICS is SQL standard-compliant, its diagnostics variables (e.g., message_text) are contextual to GSD only. So if you have a field named message_text in your table, there’s no chance that GSD can interfere with your field’s value.

    Still no line number though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to PostgreSQL. I have tables like: CREATE TABLE Person ( ID SERIAL
I'm new to postgreSQL and I have a simple question: I'm trying to create
I have a migration that runs an SQL script to create a new Postgres
New to javascript/jquery and having a hard time with using this or $(this) to
I set up a set of partitioned tables per the docs at http://www.postgresql.org/docs/8.1/interactive/ddl-partitioning.html CREATE
I am new to Django, i was going through this Django site for building
I realize that this question is slightly server related and could be posted on
New install of PostgreSQL and pgAdmin on Ubuntu 11.04 64-bit. On restoring a database
I'm trying to create a new app on heroku but it seems no matter
Sorry for this trivial question, but I am new to programming and I wanted

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.