如何在Postgresql中返回多个插入的ID?

时间:2018-12-10 23:30:02

标签: postgresql plpgsql

我有一个带有两个参数的函数,第一个是整数,第二个是varchars数组。

我只想插入之前未为广告系列插入的哈希值,然后返回插入的ID(在这种情况下,返回class NameForm extends React.Component { constructor(props) { super(props); this.state = {value: ''}; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({value: event.target.value}); } handleSubmit(event) { alert('A name was submitted: ' + this.state.value); event.preventDefault(); } render() { return ( <form onSubmit={this.handleSubmit}> <label> Name: <input type="text" value={this.state.value} onChange={this.handleChange} /> </label> <input type="submit" value="Submit" /> </form> ); } } 表的url_hash字段),但我一直在以下错误:

campaigns_urls

我正在调用这样的函数:

ERROR:  column "hash" does not exist LINE 10:  RETURNING "hash"
                    ^ 
HINT:  There is a column named "hash" in table "*SELECT*", but it cannot be referenced from this part of the query.

1 个答案:

答案 0 :(得分:1)

还有更多问题:

  1. 如果函数返回多行,则应在SETOF之后使用RETURNS关键字。

  2. PlpgSQL函数需要RETURN语句-在这种情况下为RETURN QUERY

    create table test(a int);
    
    create or replace function foo(int)
    returns setof int as $$
    begin
      return query
        insert into test 
          select v from generate_series(1,$1) g(v)
          returning a;
    end;
    $$ language plpgsql;
    
    postgres=# select * from foo(3);
    ┌─────┐
    │ foo │
    ╞═════╡
    │   1 │
    │   2 │
    │   3 │
    └─────┘
    (3 rows)