CREATE VIEW specifies more column names than columns

前端 未结 1 585
遥遥无期
遥遥无期 2021-01-27 18:26

If I run the following statements in PostgreSQL 9.4.8, I get this error message:

CREATE VIEW specifies more column names than columns.

相关标签:
1条回答
  • 2021-01-27 18:46

    Consider the simple example:

    postgres=# create function foofunc() returns table(a int, b text) language sql as $$ select 1, 'a'::text $$;
    postgres=# select foofunc();
    ╔═════════╗
    ║ foofunc ║
    ╠═════════╣
    ║ (1,a)   ║
    ╚═════════╝
    

    When a function called in the column/variable context it returns the single value of the returning type specified. Here is the source of the error: the view's select returns only one column.

    However if function called in the table context then it returns the values like a true table:

    postgres=# select * from foofunc();
    ╔═══╤═══╗
    ║ a │ b ║
    ╠═══╪═══╣
    ║ 1 │ a ║
    ╚═══╧═══╝
    

    So you should to use the second approach when you creating the view:

    CREATE VIEW v1 (c1, c2, c3, c4, c5) AS
      SELECT * FROM f1 (1, 2);
    
    0 讨论(0)
提交回复
热议问题