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”的消息?
- 如何通过异常信息引发错误?
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:
- How to print a message like 'PRINT' in T-SQL?
- How to raise errors with exception information?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要捕获错误消息及其代码:
尚未找到行号
更新2019年4月16日
根据Diego Scaravaggi的建议,对于Postgres 9.2及更高版本,请使用获取堆叠诊断:
结果:
除了 < code>GET STACKED DIAGNOSTICS 符合 SQL 标准,其诊断变量(例如,
message_text
)仅与 GSD 相关。因此,如果您的表中有一个名为message_text
的字段,GSD 就不可能干扰您的字段值。但仍然没有行号。
To catch the error message and its code:
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:
Result:
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 namedmessage_text
in your table, there's no chance that GSD can interfere with your field's value.Still no line number though.
使用 DO 语句,这是 9.0 版本中的新选项:
Use the DO statement, a new option in version 9.0:
例如,您可以将其编写为 psql 脚本,
并在运行时使用
参数引发错误,这在 PostgreSQL 中是不可能直接实现的。在移植此类代码时,有些人将必要的信息编码在错误字符串中,并在必要时将其解析出来。
这一切的运作方式都有点不同,所以准备好重新学习/重新思考/重写很多东西。
You could write this as a psql script, e.g.,
and run with
Raising errors with parameters isn't possible in PostgreSQL directly. When porting such code, some people encode the necessary information in the error string and parse it out if necessary.
It all works a bit differently, so be prepared to relearn/rethink/rewrite a lot of things.
只是想在这个旧帖子上添加我的两分钱:
在我看来,几乎所有关系数据库引擎都在执行 DDL 命令后自动执行提交事务,即使你有 autocommit=false,所以你不需要启动一个事务以避免潜在的截断对象创建,因为这是完全没有必要的。
Just want to add my two cents on this old post:
In my opinion, almost all of relational database engines include a commit transaction execution automatically after execute a DDL command even when you have autocommit=false, So you don't need to start a transaction to avoid a potential truncated object creation because It is completely unnecessary.