在SqlServer中用自訂函數返回動態表內容

來源:互聯網
上載者:User

 說明:在SupplyPlan表中,儲存著每一個RequestQty及其對應的開始終止日期段;因為我在以後處理中要判斷當前天屬於哪一條RequestQty的日期區間並進行處理,所以後台資料庫只能設計成這種儲存形式;但是在頁面的顯示時候,需要動態根據每一個SupplyPlanNo產生對應的多條日期區段及其數量顯示,所以採用自訂函數形式返回處理以後的動態記錄集!返回的欄位@AllRequestQty為"1|13|5|8"形式,在asp中就可以採用split函數分組從而動態顯示欄位。

/*
** add by wls For: convert many lines of SP datas to one char group by vehicle,part
*/
CREATE FUNCTION xzc.fn_getContent_SupplyPlan
 (@s_p_no   int = NULL,
  @viewFlag varchar(1) = ''
 )
RETURNS @fn_getContent_SupplyPlan TABLE
 (
  ID    varchar(8000),
  SupplyPlanNo   int,
  Vehicle     varchar(25),
     Sort         varchar(20),
     Part         varchar(25),
     RequestQty   varchar(8000),
     Remark   varchar(255)
 )
AS
  BEGIN
 
   DECLARE @sVehicle  varchar(25), @sSort  varchar(20)
   DECLARE @sPart  varchar(25), @iVehicle  varchar(25)
   DECLARE @iPart  varchar(25), @sRemark varchar(255)
   
   DECLARE @RequestQty  int    , @theID int
   DECLARE @AllRequestQty varchar(8000), @allID varchar(8000)
   
   select @AllRequestQty = ''
   select @allID         = ''
   
   /*Outer CURSOR*/
   DECLARE getVehiclePartSP CURSOR SCROLL FOR
  select distinct Vehicle, Sort, Part from SupplyPlan
     where SupplyPlanNo = @s_p_no and ViewFlag = @viewFlag

/*      --Order by ID desc
  select Vehicle, Sort, Part from (select min(ID) as ID, Vehicle, Sort, Part from SupplyPlan
     where SupplyPlanNo = @s_p_no group by Vehicle, Sort, Part) a order by a.ID desc
*/
   
   /*Inner CURSOR*/
   DECLARE getContentSP CURSOR SCROLL FOR
    select ID, Vehicle, Part, RequestQty
    from SupplyPlan where SupplyPlanNo = @s_p_no and ViewFlag = @viewFlag order by ID
   
    if @s_p_no IS NULL or @s_p_no = ''
    begin
   RETURN
  end
 
   OPEN getVehiclePartSP
   
    FETCH first from getVehiclePartSP into @sVehicle,@sSort,@sPart
    while(@@fetch_status = 0)
      BEGIN
      OPEN getContentSP
       FETCH first from getContentSP into @theID, @iVehicle, @iPart, @RequestQty
       while(@@fetch_status = 0)
        BEGIN
         if @iVehicle = @sVehicle and @iPart = @sPart
         begin
          select @allID         = @allID + '|' + convert(varchar(15), @theID)
          select @AllRequestQty = @AllRequestQty + '|' + convert(varchar(25), @RequestQty)
         end
         FETCH next from getContentSP into @theID, @iVehicle, @iPart, @RequestQty
        END
      CLOSE getContentSP
      
    select @allID     = xzc.trim_char(@allID, '|')
      select @AllRequestQty = xzc.trim_char(@AllRequestQty, '|')
      select @sRemark       = (select top 1 Remark from SupplyPlan where SupplyPlanNo = @s_p_no
         and Vehicle = @sVehicle and Part = @sPart)
      
      INSERT INTO @fn_getContent_SupplyPlan VALUES(@allID, @s_p_no, @sVehicle, @sSort, @sPart, @AllRequestQty, @sRemark)

      select @AllRequestQty = ''
      select @allID         = ''
      select @sRemark       = ''
      
      FETCH next from getVehiclePartSP into @sVehicle,@sSort,@sPart
     END
      DEALLOCATE getContentSP
    
   CLOSE getVehiclePartSP
   DEALLOCATE getVehiclePartSP
 
 
  RETURN
  END

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.