PostgreSQL异常处理

前端之家收集整理的这篇文章主要介绍了PostgreSQL异常处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是Postgresql的新手。任何人都可以更正此查询
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;

问题:

>如何在T-sql中打印“PRINT”等消息?
>如何提出异常信息的错误

要捕获错误消息及其代码
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';

还没有找到行号

原文链接:https://www.f2er.com/postgresql/193025.html

猜你在找的Postgre SQL相关文章