2010-04-21 2 views
1

저는 Postgres에서 일하고 있는데, 많은 행과 열이있는 전체 결과 집합을 저장 프로 시저 또는 함수로 보내야합니다. 이것이 가능한가? 그렇다면 구문에 대한 리소스는 어디서 볼 수 있습니까?전체 결과 집합을 SQL 함수로 보낼 수 있습니까?

OK 이것은 결과 집합을 보내지 않고 설정 한 방법입니다. 비교 논리를 깨고 두 개의 다른 지점에 넣으려고합니다.하지만 내 목표는 실제 프로모션을 찾는 것입니다. 한 곳에서의 논리, 내가 여기에서 한 것. 이것은 언젠가 바뀔지도 모릅니다. 비교 논리가 변하기 쉽지 않습니다. 그것은 꽤 표준입니다.

추진 품목 로직

저기가 될 트리거가 promo_objects, promo_buy_objects 및 promo_get_objects 테이블에 대한 INSERT에 설정합니다

, 프로모션 테이블에 UPDATE 트리거가있을 것입니다. 외부 참조에 대한 년 - 트리거는 그 목적에 가장 적합한 홍보를 결정하는 저장 프로 시저라는 set_best_product_promos를 호출하고 그 다음에 새 테이블에 저장됩니다 :

promo_best_product_promos

PROMO_ID, OBJECT_ID, 만료

update_best_product_promos를 호출하고 PROMO_ID에서 활성 경우 보내드립니다 프로모션에 대한 -THE 트리거 = 사실은 그렇지가 프로모션

에 대한 모든 항목을 삭제하는 프로모션의 만료 날짜를 업데이트합니다

새 테이블이 promo.sql 스크립트에 추가되었지만 함수가 작성 될 때까지 트리거 및 함수를 추가 할 수는 없습니다.

스크립트는 매일 밤 자정에 만료되어 만료 된 항목을 삭제합니다. 카트 코드 (응용 프로그램 코드)에 대한 PSEUDO 우리가 지금 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 

편집 : 그래서 내가 같이이 카트 논리를 덤프 수도 있겠죠 스토어드 프로 시저뿐만 아니라 비교 로직을 위해 하나를 만들고 스토어드 프로 시저와 포터블 및 제네릭 모두에서 호황을 누릴 수 있습니까? set_best_product_promos에 대한

PSEUDO : get_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 

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 

답변

1

cursors를 살펴 보자.

정의 함수를 사용하면 문서를 확인할 수 있습니다 PL/pgSQL에 대한 입력 및 출력 매개 변수의 형식에 많은 languages

작성할 수 있습니다

+0

그래서 FETCH를 사용하려면 커서를 두 개 이상 호출해야하는 쿼리가 필요합니다. 이 문서의 다음 결과를 얻을 수 FETCH 말을, 그래서 당신은 커서 기능 루프 에서 선택 변수 끝 루프 에 커서를 가져올 것인가? – KacieHouser

1

포스트 그레스 사용자 here

는 당신이이를 통과해야 당신이 있는지 있습니까 함수? 이 문제를 피하기 위해 함수를 구조화 할 수 있다고 믿습니다. 함수는 테이블을 반환하고 테이블을 가져올 수 있습니다.이 테이블이 쿼리/테이블/뷰인 경우 함수 내에서 SQL을 사용하여 다른 데이터 형식의 매개 변수 만 전달할 수 있습니다. 이 테이블이 다른 함수의 결과 일 경우 함수를 호출하여 테이블에 도달 할 수 있습니다. 너의 시나리오는 뭐니?

+0

제품에 대한 최고의 프로모션을 찾아야합니다. 따라서 제품에는 많은 프로모션이 묶여있을 수 있습니다. 두 개의 서로 다른 저장소에서이 작업을 수행해야하므로 SQl 함수를 처리하도록 결정했습니다. 하나의 레포에서는 제품에 가장 적합한 프로모션이 무엇인지 알 필요가 있습니다. 내가 장바구니에있을 때 광고 항목에 가장 적합한 프로모션이 무엇인지 알아야하지만 반환 된 프로모션 중 일부는 카트의 해당 항목에 적용되지 않을 수 있습니다 (구매 4는 장바구니에 1 개만있는 경우 20 % 할인 그런 다음 해당 프로모션을 적용 할 수 없음) – KacieHouser

+0

어떤 프로모션이 최선인지 알아 내기 전에 카트 컨텍스트에서 프로모션을 필터링해야합니다. 따라서 나는 그 함수 내의 모든 프로모션을 쿼리 할 수없고 같은 함수에서 모두 반복 할 수 없다. 장바구니에 뭐가 있는지 알아야 해. – KacieHouser

+0

그래서 한 가지 기능으로 해당 항목에 대한 프로모션을 쿼리해야합니다. 예를 들어 코드에서 일부 로직을 수행하고 편집 된 결과를 다른 기능으로 보내 어떤 프로모션이 가장 효과적인지 판단합니다. – KacieHouser

관련 문제