SQL 使用 RETURNING 子句进行变量赋值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3687906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Variable value assignment using RETURNING clause
提问by Omu
I try to do this, but it's a syntax error, what am I doing wrong?
我尝试这样做,但这是一个语法错误,我做错了什么?
declare myid := insert into oameni values(default,'lol') returning id;
my table:
我的表:
create table oameni
(
id serial primary key,
name varhcar(10)
);
回答by OMG Ponies
You need to use the INTO clause in the RETURNING to set the value being returned into your variable:
您需要在 RETURNING 中使用 INTO 子句来设置返回到变量中的值:
DECLARE myid OAMENI.id%TYPE;
INSERT INTO oameni
VALUES
(default,'lol')
RETURNING id INTO myid;
You also need to specify the data type of your variable; I'm glad to see postgresql supports %TYPE and %ROWTYPE.
您还需要指定变量的数据类型;我很高兴看到postgresql 支持 %TYPE 和 %ROWTYPE。