微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – 被遗忘的赋值运算符“=”和普通的“:=”

PL / pgsql的文档说,声明和赋值给变量是:=。
一个简单,更短,更现代(看脚注)=似乎工作正如预期:
CREATE OR REPLACE FUNCTION foo() RETURNS int AS $$
    DECLARE
      i int;
    BEGIN
      i = 0;  
      WHILE NOT i = 25 LOOP
          i = i + 1;
          i = i * i;
      END LOOP;
      RETURN i;
    END;
    $$ LANGUAGE plpgsql;

    > SELECT foo();
    25

请注意,Pl / pgsql可以清楚地区分赋值和比较,如图所示

WHILE NOT i = 25 LOOP

所以,问题是:

>我没有在文档中找到提及和/或解释这一点的一些部分?
>有没有任何已知的后果使用=而不是:=?

编辑/脚注:

请采取“更现代”的部分与眨眼像A Brief,Incomplete,and Mostly Wrong History of Programming Languages

1970 – Niklaus Wirth creates Pascal,a procedural language. Critics
immediately denounce Pascal because it uses “x := x + y” Syntax
instead of the more familiar C-like “x = x + y”. This criticism
happens in spite of the fact that C has not yet been invented.

1972 – Dennis Ritchie invents a powerful gun that shoots both forward
and backward simultaneously. Not satisfied with the number of deaths
and permanent maimings from that invention he invents C and Unix.

在PL / Pgsql解析器中,赋值运算符被定义为
assign_operator : '='
                | COLON_EQUALS
                ;

这是一个未记录的旧功能。至少自1998年以来在Postgresql代码中。
它计划被删除,但仍然保持,因为有些人依赖它。

查看Tom Lane的消息(核心Pg开发人员):
http://archives.postgresql.org/pgsql-bugs/2011-08/msg00140.php

首先介绍一下(根据官方Git仓库):
http://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=863a62064cfc2b706dd6ab45487d15cc33cedb35

所以,直接回答你的问题:

Didn’t I find some section in the docs which mention and/or explains
this?

你没有找到它,因为它是无证的,不应该依赖。

Are there any kNown consequences using = instead of :=.

使用=没有任何副作用,但是你应该使用:=作为赋值,使你的代码面向未来。

更新:在罕见的情况下可能会有副作用(见Erwin的回答)

UPDATE(2015年6月):正如Daniel指出的,截至Postgresql 9.4 it is documented.这个特性可能会比我预期的更长。这可能不仅是一个遗留功能,而且还是PL / sql兼容性。

原文地址:https://www.jb51.cc/postgresql/193464.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐