1

私は Postgres で作業しており、多くの行と列を含む完全な結果セットをストアド プロシージャまたは関数に送信する必要があります。これは可能ですか?その場合、構文のリソースはどこで確認できますか?

これは、結果セットを送信できずに設定した方法です。比較ロジックを分割して2つの異なる場所に配置する必要がありますが、私の目標は、プロモーションロジックを実際に見つけて1か所に保つことです、ここで行いました。これはいつか変更される可能性がありますが、比較ロジックは変更される可能性が低く、かなり標準的です。

プロモーション広告申込情報のロジック

-promo_objects、promo_buy_objects、および promo_get_objects テーブルの INSERT にトリガーが設定され、promo テーブルに UPDATE トリガーが設定されます。-xrefs のトリガーは、set_best_product_promos というストアド プロシージャを呼び出します。このストアド プロシージャは、そのオブジェクトに最適なプロモーションを決定し、新しいテーブルに保存します。

promo_best_product_promos

promo_id、object_id、expiration_date

-プロモーションのトリガーは update_best_product_promos を呼び出し、promo_id を送信します。active = true の場合、そのプロモーションの有効期限が更新されます。そうでない場合、そのプロモーションのすべてのエントリが削除されます。

新しいテーブルが promo.sql スクリプトに追加されましたが、トリガーと関数は関数が作成されるまで追加できません。

毎晩深夜にスクリプトが実行され、有効期限が切れたエントリが削除されます。PSEUDO FOR cart code (アプリケーション コード) 表示されているようにユニオン クエリを実行します。

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

編集:だから私は、このカート ロジックをストアド プロシージャとしてもダンプし、比較ロジック用に作成して、ストアド プロシージャとポータブルでジェネリックですべてブームにすることができると思いますか?

set_best_product_promos の PSEUDO:

-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

get_best_product_promos の PSEUDO:

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
4

2 に答える 2

1

カーソルを見てください。

于 2010-04-21T17:42:04.460 に答える
1

Postgresのユーザー定義関数は多くの言語で書くことができます

PL / pgSQLの入力および出力パラメータのフォーマットについては、ここでドキュメントを確認できます。

これを関数に渡す必要がありますか?これを回避するために関数を構造化できると思います。関数はテーブルを返し、その中にgetテーブルを取得できます。このテーブルがクエリ/テーブル/ビューの場合は、関数内でSQLを使用してアクセスできます(他のデータ型のパラメーターのみを渡します)。このテーブルが別の関数の結果である場合は、関数を呼び出してテーブルにアクセスできます。あなたのシナリオは何ですか?

于 2010-04-21T17:43:13.573 に答える