我是Postgres的新手,正在从事我的工作。我不得不创建一个只有1列的表,然后让我在pgadmin III上运行以下语句:
BEGIN; INSERT INTO mytable VALUES (1); SAVEPOINT savepoint1; INSERT INTO mytable VALUES (2); ROLLBACK TO SAVEPOINT savepoint1; INSERT INTO mytable VALUES (3); SAVEPOINT savepoint2; INSERT INTO mytable VALUES (4); INSERT INTO mytable VALUES (5); SAVEPOINT savepoint3; SELECT * FROM mytable; --NOTE: You need to run this IF statement as PGScript --(button next to the normal run button) IF (CAST ((SELECT MAX(id) FROM mytable) AS INTEGER) = 4) BEGIN RELEASE SAVEPOINT savepoint2; END ELSE BEGIN INSERT INTO mytable VALUES(6); END --Run the next steps normally SAVEPOINT savepoint2; INSERT INTO mytable VALUES (7); RELEASE SAVEPOINT savepoint2; INSERT INTO mytable VALUES (8); ROLLBACK TO savepoint2; COMMIT;
当我运行此命令时,出现以下错误:“ IF”或附近的语法错误
我已经看过这个38.6.2。条件句38.6.2。条件句,我对此不太了解,是否需要将查询更改为
IF (CAST ((SELECT MAX(id) FROM mytable) AS INTEGER) = 4) THEN BEGiN
然后当它结束时,我应该以:
END IF
为什么毕竟有错误?
IF和其他PL / pgSQL功能仅在PL / pgSQL函数内部可用。如果要使用,则需要将代码包装在一个函数中IF。如果您使用的是9.0+,则可以使用DO编写内联函数:
IF
DO
do $$ begin -- code goes here end $$
如果使用的是PostgreSQL的早期版本,则必须编写一个包含代码的命名函数,然后执行该函数。