Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,


I am new to postgresql. Here my task is to use exceptional handling with transaction.
Can anyone suggest me any solution to my problem.


Thank you...
Posted

1 solution

Taken from Postgresql exception-catching rocks! [^] -
SQL
begin try

    begin transaction

    -- alter table z drop column aaa;
    alter table z add zzz int;

    commit tran;

end try
begin catch

    print 'hello';
    SELECT
        ERROR_NUMBER() as ErrorNumber,
        ERROR_MESSAGE() as ErrorMessage;

    IF (XACT_STATE()) = -1
    BEGIN
        PRINT
            N'The transaction is in an uncommittable state. ' +
            'Rolling back transaction.'
        ROLLBACK TRANSACTION;
    END;

end catch

print 'reached';
 
Share this answer
 
Comments
Priyanka Tiwari001 1-Sep-15 5:24am    
Thank u for your solution but its raising error on pgadmin- postgreSQL. I think 'try' is not valid keyword in PostgreSQL because on running this query I am getting error as-

ERROR: syntax error at or near "try"
LINE 1: begin try

Could you suggest me some other solution?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900