2013-02-26 53 views
0

該計劃是創建一個包數爲no。由員工根據組織的位置採取的葉子。 例如: - 如果XYZ公司是一家位於許多地方的公司,每個地點都有自己的一套規則......在某些地點星期六工作,並且在某些星期六和星期日都被視爲假期。 我已經做了如下包,但我認爲它可以變得更容易。根據地點計算休假天數

Create or replace package Body 
Leave_location 
Procedure Leave_location_proc(Person_id number, 
           Start_date Date 
          ) 
as 
k Number; 
count_weekend number; 

Cursor c_var 
is 
select pt.person_id, 
lt.end_date-lt.start_date Number_of_leaves_excluding_the_end_date, 
lt.Start_date  //Start leave date, 
lt.Saturday  // 1 if saturday is not working, null if working 
lt.Sunday   , 
loc.location_id 
from person_table pt, 
leave_table lt 
and leave_table.person_id=person_table.person_id 
and loc.location_id=pt.person_id \\filtering the location for the particular employee; 
Begin 

Count_weekend :=0; 
     for cursor_var in c_var 
     k :=cursor_var.Number_of_leaves_excluding_the_end_date+1 
     loop 
     for i in..k 
     loop 
      if(to_char(cursor_var.start_date,'DY') in ('Sat','Sun') 
      Then 
      if(cursor_var.Saturday is not null and cursor_var.sunday is not null)) 
      Then 
      Count_weekend :=2; 
      Else 
      count_weekend :=1; 
      end if ; 
      end if; 
     cursor_var.Start_date :=cursor_var.Start_date+1; 
No_of_leaves :=K-Count_weekends; 
Count_weekends:=Count_weekends+1; 
end loop; 
end loop; 

End Leave_location; 
Leave_location_proc; 

此代碼似乎效率不高。更好的方法

+0

這看起來不像是實際上是最終並且正在運行的代碼,但您爲什麼認爲它效率低下? – 2013-02-26 16:52:53

+0

這種「感覺」就像在一個查詢中可以完成的事情,但是你的光標不是那麼清晰......你能告訴我們你的表結構並給出一些輸入例子嗎? – 2013-02-26 16:55:06

+1

查看[這篇文章](http://stackoverflow.com/questions/14898357/calculate-business-days-in-oracle-sqlno-functions-or-procedure),它可能會給你一些想法 – 2013-02-26 16:59:58

回答

0

你不需要PL/SQL。這是我的建議 - 這只是查詢,可以轉換爲光標。您可以計算查詢中所需的全部內容。然後,如果需要,您可以在光標中使用它:

-- calculate values from inner/main query 
SELECT all_needed_columns_from_inner_query 
    , (Number_of_leaves_excluding_the_end_date - nbr_of_wkends) whatever_count 
    FROM 
(-- your main query -- 
    SELECT ... 
     , lt.end_date-lt.start_date Number_of_leaves_excluding_the_end_date 
     , (CASE WHEN lt.Saturday IS NOT NULL AND lt.Sunday IS NOT NULL THEN 2 ELSE 1) nbr_of_wkends 
    ... 
WHERE... 
) 

如果您需要更多幫助,請發佈示例數據。但上面的例子應該給你明確的方向和想法 - 使用CASE來計算你需要的所有值。在PL/SQL中只計算那些絕對無法用SQL計算的值,無論出於何種原因...