尝试从更新设置变量不起作用
Trying to set variable from update doesn't work
谁能指出以下代码有什么问题吗?
它看起来像是完全合法的语法,但是当我 运行 它时,我得到:
ERROR: syntax error at or near "conctest"
LINE 7: ret_id := (update conctest set id=id+1 where name = 'Billy ...
^
********** Error **********
ERROR: syntax error at or near "conctest"
SQL state: 42601
Character: 66
损坏的代码:
DO $$
declare ret_id integer;
begin
ret_id := (update conctest set id=id+1 where name = 'Billy Bob' returning id);
DROP TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE tmpTable AS
select ret_id;
END $$;
select * from tmpTable;
这个有效:
DO $$
declare ret_id integer;
begin
update conctest set id = id + 1 where name = 'Billy Bob';
ret_id := (select id from conctest where name = 'Billy Bob');
DROP TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE tmpTable AS
select ret_id;
END $$;
select * from tmpTable;
顺便说一下,我从工作 SQL 查询 window 中复制了更新子句并手动添加了 " returning id"
因此其中没有奇怪的字符。
怎么了?
TVMIA,
亚当。
根据docs:
UPDATE ... RETURNING expressions INTO [STRICT] target;
尝试使用 into
代替:
t=# create table s111(i int);
CREATE TABLE
Time: 37.521 ms
t=# insert into s111 select 1;
INSERT 0 1
Time: 1.016 ms
t=# do
$$
declare _i int;
begin
update s111 set i =2 returning i into _i;
raise info '%',_i;
end;
$$
;
INFO: 2
DO
Time: 0.944 ms
谁能指出以下代码有什么问题吗? 它看起来像是完全合法的语法,但是当我 运行 它时,我得到:
ERROR: syntax error at or near "conctest"
LINE 7: ret_id := (update conctest set id=id+1 where name = 'Billy ...
^
********** Error **********
ERROR: syntax error at or near "conctest"
SQL state: 42601
Character: 66
损坏的代码:
DO $$
declare ret_id integer;
begin
ret_id := (update conctest set id=id+1 where name = 'Billy Bob' returning id);
DROP TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE tmpTable AS
select ret_id;
END $$;
select * from tmpTable;
这个有效:
DO $$
declare ret_id integer;
begin
update conctest set id = id + 1 where name = 'Billy Bob';
ret_id := (select id from conctest where name = 'Billy Bob');
DROP TABLE IF EXISTS tmpTable;
CREATE TEMPORARY TABLE tmpTable AS
select ret_id;
END $$;
select * from tmpTable;
顺便说一下,我从工作 SQL 查询 window 中复制了更新子句并手动添加了 " returning id"
因此其中没有奇怪的字符。
怎么了?
TVMIA,
亚当。
根据docs:
UPDATE ... RETURNING expressions INTO [STRICT] target;
尝试使用 into
代替:
t=# create table s111(i int);
CREATE TABLE
Time: 37.521 ms
t=# insert into s111 select 1;
INSERT 0 1
Time: 1.016 ms
t=# do
$$
declare _i int;
begin
update s111 set i =2 returning i into _i;
raise info '%',_i;
end;
$$
;
INFO: 2
DO
Time: 0.944 ms