Oracle - 创建一个从多个表插入维度表的过程

时间:2014-11-16 02:58:08

标签: sql oracle plsql procedures

我正在为小型电子商务设计数据仓库,我需要为两个表product和product_price_history的产品填充维度表。为简单起见,我们假设每个表的列如下:

产品(prod_id,品牌) product_price_history(prod_id,price,min_pirce,start_date,end_date) product_dim(prod_key,prod_id,brand,price,min_pirce,start_date,end_date)

我已经有一个名为prod_seq的序列来为product_dimension表生成键。这是我到目前为止我的程序,但我',得到错误:

create or replace 
procedure populate_product_dimension AS
BEGIN
INSERT into product_dim (prod_key, prod_id, brand, price, min_pirce, start_date, end_date)
SELECT seq_prod.nextval FROM DUAL
SELECT prod_id, brand
FROM product
SELECT price, min_pirce, start_date, end_date
FROM product_price_history
WHERE prod_id.product_pric_history = prod_id.product &
prod_id not in(select prod_id from product_dim);
Commit;
End populate_product_dimension;

我认为问题可能是由于我如何添加prod_key。任何人都知道更好的方法吗?

1 个答案:

答案 0 :(得分:1)

您用于SELECT

INSERT查询。它必须是JOIN并且您的语法错误。引用列时必须是table_name / alias.column_name。

create or replace 
procedure populate_product_dimension AS
BEGIN
INSERT into product_dim (prod_key, prod_id, brand, price, min_pirce, start_date, end_date)
SELECT 
       seq_prod.nextval,
       p.prod_id,
       p.brand,
       ph.price,
       ph.min_pirce,
       ph.start_date,
       ph.end_date
FROM 
       product_price_history ph,
       product p
WHERE 
      ph.prod_id = p.prod_id
  AND ph.prod_id not in(select prod_id from product_dim);

Commit;
End populate_product_dimension;