【问题标题】:If statements in postgres throw error如果 postgres 中的语句抛出错误
【发布时间】:2013-03-29 19:07:31
【问题描述】:

我正在尝试对此发表声明:

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;

它会抛出错误:

错误:“IF”处或附近的语法错误

我找到了this question,但这些答案中建议的格式似乎也不起作用。想知道是否有人能告诉我为什么这不起作用?

【问题讨论】:

  • 你有一个双;; - 不确定这很重要..

标签: database postgresql if-statement


【解决方案1】:

If 是 plpgsql 语言的一部分。它只能在 plpgsql 函数中工作:

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

【讨论】:

    【解决方案2】:

    您正在寻找CASE 表达式。

    【讨论】:

    • 不过,我认为 case 不会允许我做同样的事情?
    • 其实CASE,好像不想用,除非它已经在另一个语句中了....
    猜你喜欢
    • 2015-01-05
    • 1970-01-01
    • 2020-12-09
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2018-03-01
    相关资源
    最近更新 更多