加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-13 16:46:19 所属栏目:百科 来源:网络整理
导读: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;
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兼容性。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读