如何在存储过程中编写创建表语句

时间:2018-07-20 22:56:14

标签: postgresql stored-procedures

如何在postgresql的存储proc中编写以下查询?

 create table data1 as
 select A.*,
 case when score >=940 then 1
 when score between 600 and 746 then 2
 when bureau_score between 599 and 630 then 4 else 5 end as score_level,
 case when band between -1 and 5 then 1
 when band between 6 and 20 then 2
 when band between 21 and 35 then 3 else 4 end as band_level
 from data A;

1 个答案:

答案 0 :(得分:1)

PostgreSQL没有这样的存储过程,只有函数。

如果是简单的SQL,则只需将其包装在SQL函数定义中即可。

create or replace function foo () returns void language sql as $$

  create table data1 as
  select A.*,
  case when score >=940 then 1
  when score between 600 and 746 then 2
  when bureau_score between 599 and 630 then 4 else 5 end as score_level,
  case when band between -1 and 5 then 1
  when band between 6 and 20 then 2
  when band between 21 and 35 then 3 else 4 end as band_level
  from data A;

$$;

要调用它,请SELECT foo();