2010-04-21 57 views
1

我在Postgres工作,我需要发送一个完整的结果集与许多行和列到存储过程或函数。这可能吗?如果是这样,我在哪里可以看到语法资源?您可以将完整的结果集发送到SQL函数吗?

好的,这是我无法发送结果集而设置的,它迫使我打破比较逻辑并将其放在两个不同的位置,但我的目标是保持实际发现的促进我在这里完成了一个地方的逻辑。这可能会改变一天,比较逻辑不太可能改变,这是相当标准的。

促进行项目逻辑

-there将在INSERT为promo_objects,promo_buy_objects和promo_get_objects表设置为触发器,将有对电视节目预告表中的UPDATE触发器。 的外部参照-The触发器将调用存储过程调用set_best_product_promos将决定哪些促销是最好的那个对象,然后将它保存到一个新表:

promo_best_product_promos

PROMO_ID, OBJECT_ID, 到期日期

-The触发宣传片将调用update_best_product_promos,将在PROMO_ID如果主动发送=真会更新该促销的截止日期否则将删除所有条目为促销

新表已添加到promo.sql脚本中,但只有在写入函数后才能添加触发器和函数。

脚本将在每天午夜运行以删除已过期的条目。 伪推车代码(应用程序代码) 运行联合查询,就像我们现在shown_object_promotions(这得到所有可用的促销活动的项目)

Loop through results 
    if buy_quantity > 0 
     IF the quantity of the buy item in the cart is greater than or = the buy_quantity (I think c.active_items is the items in the cart) 
      IF get_quantity > 0 
      If the get item is in the cart AND it is the item sent into this function (I think c.active_items is the items in the cart) 
       run the get_best_product_promos function 
       run comparison logic 
      else 
      run the get_best_product_promos function 
      run comparison logic 

编辑:所以我想我可以放弃这个车逻辑一个存储过程,然后为比较逻辑做一个,并在存储过程和便携式和泛型中繁荣它的一切?

拟FOR set_best_product_promos:

-You will send in the object_id and promo_id 
-You will declare all of your variables 
-Go ahead an query the end date of the promo 
-You will then query the promo_best_product_promos table to see if an entry exists for this product 

IF exists: 
    RUN YOUR UNION QUERY accept this time you will have to explicitly say all the fields you want and what variables to select them into 

    Then loop through your query 
    LOOP 
     run get_best_product_promos 
     run comparison logic 
    END LOOP 

    Now take those variables you set in the crazy logic and update promo_best_product_promos 
ELSE: 
    insert the object_id, promo_id, and end date (expiration_date) into the promo_best_product_promos table 

拟FOR get_best_product_promos:

If no buy and no get quantities 
    If discount type = percent 
     calculate value of the promotion for this item to compare later 
     calculate the new price for the product and update the estimated unit price 
    If discount type = dollar 
     calculate value of the promotion for this item to compare later 
     calculate the new price for the product and update the estimated unit price 
    If discount type = price 
     calculate value of the promotion for this item to compare later 
     calculate the new price for the product and update the estimated unit price 
    If discount amount = Free 
     do nothing 
     pass 
    If buy quantity but no get quantity 
     If discount type = percent 
     calculate value of the promotion for this item to compare later 
     If discount type = dollar 
     calculate value of the promotion for this item to compare later 
     If discount type = price 
     calculate value of the promotion for this item to compare later 
     If discount amount = Free 
     do nothing 
     pass 
    Else (assumes there is both buy and get) 
    IF the quantity of the buy item in the cart is >= the buy_quantity (I think c.active_items is the items in the cart) 
     If discount type = percent 
      calculate value of the promotion for this item to compare later 
     If discount type = dollar 
      calculate value of the promotion for this item to compare later 
     If discount type = price  
      calculate value of the promotion for this item to compare later 
     If discount amount = Free 
     #Use a different var here like in select_cart_promotion - they will always get this promotion 
     calculate the value of the promotion for these items 
     do something here to ensure the get product is in the cart 

回答

1

看看cursors

+0

所以我必须使用FETCH,因为我正在做的查询w /游标需要从多个地方调用。它说FETCH将获得文档中的下一个结果,所以你会 从光标功能 环 选择取光标移到变量 结束循环 ? – KacieHouser 2010-04-22 18:54:25

1

Postgres用户定义的函数可以用在许多languages

关于为PL/pgSQL的输入和输出参数的格式,你可以查看文档here

你确定你需要这个传递给一个函数?我相信你可以通过构造你的函数来避免这种情况,函数可以返回表并获取表中的表。如果你的这个表是一个查询/表/视图,那么你可以在函数内部使用SQL来获取它(只传递其他数据类型的参数);如果这个表是另一个函数的结果,你可以调用函数来访问表。你的场景是什么?

+0

我需要找到产品上的最佳促销。因此,产品可能会有许多与其相关的促销活动。我需要从两个不同的存储库中做到这一点,因此,我决定让SQl的功能来处理它。 在一个回购中,我只需要知道什么是最好的促销产品。当我在购物车中时,我需要知道某个订单项的最佳促销活动,但返回的促销活动中的一些可能不适用于购物车中的该商品(如果购物车中只有1个,请购买4折优惠,那么我无法申请该促销活动) – KacieHouser 2010-04-21 18:42:01

+0

因此,在确定哪些促销活动最好之前,有必要过滤购物车上下文中的促销活动。因此,我不能只是查询该函数中的所有促销活动,并在同一个函数中循环全部。我必须知道购物车中有什么。 – KacieHouser 2010-04-21 18:43:05

+0

所以我需要在一个函数中查询该项目的促销活动,在购物车的情况下,我会在我的代码中执行一些逻辑并将这些编辑后的结果发送给另一个函数,以确定哪一个是最好的促销活动。 – KacieHouser 2010-04-21 18:49:46

相关问题