1

I'm trying to run a statement to this effect:

IF EXISTS (SELECT * FROM LeadCustomer WHERE FirstName = 'John' AND Surname = 'Smith') THEN
    SELECT * FROM LeadCustomer WHERE FirstName = 'John' AND Surname = 'Smith';
ELSE 
    INSERT INTO LeadCustomer (Firstname, Surname, BillingAddress, email) VALUES ('John', 'Smith', '6 Brewery close, Buxton, Norfolk', '[email protected]');;
END IF;

and it throws the error:

ERROR: syntax error at or near "IF"

I found this question but the format suggested in those answers don't seem to work either. Was wondering if anyone could tell me why this isn't working?

1
  • you have a double ;; - not sure that matters.. Commented Mar 29, 2013 at 19:12

2 Answers 2

2

If is part of the plpgsql language. It will only work inside a plpgsql function:

http://www.postgresql.org/docs/current/interactive/plpgsql.html

Sign up to request clarification or add additional context in comments.

Comments

2

You are looking for the CASE expression.

2 Comments

I don't think case would allow me to do quite the same thing, though?
In fact, CASE, doesn't seem to want to be used unless it's already in another statement....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.