Postgresql Syntax Error At Or Near If
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn postgres iferror more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
Postgres Syntax Error At Or Near Case
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like postgres if statement example you, helping each other. Join them; it only takes a minute: Sign up Postgres syntax error at or near “IF” up vote 8 down vote favorite I am new to postgres and I am working on an assignment psql do of mine. I had to create a table with only 1 column and, then I was given this statement to run in on 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
If Exists Postgresql
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; when I run this I get this error: syntax error at or near "IF" I have already take a look at this 38.6.2. Conditionals38.6.2. Conditionals , I dont understand this very well, Do I need to change the query to have IF (CAST ((SELECT MAX(id) FROM mytable) AS INTEGER) = 4) THEN BEGiN and then when it ends I should end it with: END IF Why there is an error after all?? sql postgresql if-statement share|improve this question asked Jan 6 '14 at 19:03 S. N 34221129 You're missing THEN... It's IF ... THEN ... ELSE .... You've got IF ... ... ELSE. –Marc B Jan 6 '14 at 19:05 @MarcB I tried it with THEN but still I get error –S. N Jan 6 '14 at 19:08 add a comment| 1 Answer 1 active oldest votes up vote 16 down vote accepted IF and other PL/pgSQL features are only available inside PL/pgSQL functions. You need to wrap your code in a function if you want to use IF
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about query has no destination for result data Stack Overflow the company Business Learn more about hiring developers or posting ads with postgres case us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is
Postgres Functions
a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PostgreSQL “IF” syntax error up vote 11 down vote favorite 2 I'm new with http://stackoverflow.com/questions/20957292/postgres-syntax-error-at-or-near-if PostgreSQL, and I already have my first problem.. I wrote some code to understand how transactions work, following the manual step by step. To make it short, I've created 2 tables, user and movements: in the first one there are the name, email and credit columns, in the second the columns from, to, import. So, I was trying this way: BEGIN; INSERT INTO movements (from, to, import) VALUES ('mary', 'steve', http://stackoverflow.com/questions/802764/postgresql-if-syntax-error 600); UPDATE users SET credit = credit - 600 WHERE name = 'mary'; UPDATE users SET credit = credit + 600 WHERE name = 'steve'; --here comes the problem! IF (SELECT credit FROM users WHERE name = 'mary') < 0 THEN ROLLBACK; END IF COMMIT; I always get the error: ERROR: syntax error at or near "IF" Where am I mistaken? P.S.: Don't focus on the example functionality, it's just a trial for me to understand the transactions.. and now, the IF clause... sql postgresql syntax if-statement share|improve this question edited Mar 1 '13 at 21:02 Will Ness 30.6k45188 asked Apr 29 '09 at 14:58 Strae 8,5901764117 I have added sample code, by your request. This ought to help you out. :-) –pyrocumulus Apr 29 '09 at 21:58 I think others have answered what the problem with the syntax is, but I have a comment. It looks like the invariant that your code is trying to maintain could be maintained with a check constraint on the credit column. –Warren Apr 30 '09 at 4:41 Use a check constraint, then you don't need this construction. –Frank Heikens Apr 29 '10 at 8:56 @frank: i was trying to learn hot transactions works
pgsql-announce pgsql-bugs pgsql-docs pgsql-general pgsql-interfaces pgsql-jobs pgsql-novice pgsql-performance pgsql-php pgsql-sql pgsql-students Developer lists Regional lists Associations User groups Project lists Inactive lists IRC Local User Groups Featured Users International https://www.postgresql.org/message-id/b3bd908e-4245-44aa-8ee4-704c73147768@y6g2000prf.googlegroups.com Sites Propaganda Resources Weekly News Re: ERROR: syntax error at or near "IF"... why? From: "DaNieL(dot)(dot)!"