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', 'cmp.testing@example.com');;
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?

Community
  • 1
  • 1
The General
  • 1,239
  • 2
  • 17
  • 28

2 Answers2

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

Clodoaldo Neto
  • 118,695
  • 26
  • 233
  • 260
2

You are looking for the CASE expression.

Chris Farmiloe
  • 13,935
  • 5
  • 48
  • 57