sql server遊標暫存資料表

來源:互聯網
上載者:User
 

ALTER PROCEDURE MKTG_DiffeRenceDailyReport
@CustomerName varchar(100),
@ShipDate datetime
AS
BEGIN
 Set NoCount ON
 
  DECLARE
  @NoReceivedPackages varchar(2000),
  @CurrBillTrackId varchar(30),
  @CurrPackageId varchar(30)
 
 SELECT a.BillTrackId, a.Pieces, (case isnull(a.Canceled, 0) when 1 then '√' end) as Canceled,

 c.FacilityName AS CustomerName, a.ShipDate,
 CAST('' as varchar(2000)) AS NoReceivedPackageIdList,
 COUNT(b.PackageTrackId) AS ReceivedPieces
 INTO #t1
 FROM MKTG_ShipBills a
 LEFT JOIN MKTG_ShipmentsCheckin b ON a.BillTrackId = b.BillTrackId
 LEFT JOIN Common_Facilities c ON a.CustomerId = c.FacilityId
 WHERE c.FacilityName = @CustomerName AND a.ShipDate = @ShipDate
 GROUP BY a.BillTrackId, a.Pieces, a.Canceled, c.FacilityName, a.ShipDate
 ORDER BY a.BillTrackId
 
 DECLARE c1 CURSOR FOR SELECT BillTrackId FROM #t1
 OPEN c1
 FETCH NEXT FROM c1 INTO @CurrBillTrackId
 WHILE @@FETCH_STATUS = 0
 BEGIN
   SELECT a.PackageTrackId
   INTO #t2
   FROM MKTG_ShipPackages a
  left join MKTG_ShipmentsCheckin b ON a.BillTrackId = b.BillTrackId
  left join MKTG_ShipBills d ON a.BillTrackId = d.BillTrackId
  LEFT JOIN Common_Facilities c ON c.FacilityId = d.CustomerId
  WHERE a.PackageTrackId not in (SELECT PackageTrackId FROM MKTG_ShipmentsCheckin)
  and c.FacilityName = @CustomerName AND d.ShipDate = @ShipDate
  and a.BillTrackId = @CurrBillTrackId
  SET @NoReceivedPackages = ''
  DECLARE c2 CURSOR FOR SELECT PackageTrackId FROM #t2
  OPEN c2
  FETCH NEXT FROM c2 INTO @CurrPackageId
  WHILE @@FETCH_STATUS = 0
  BEGIN
   SET @NoReceivedPackages = @NoReceivedPackages + ';' + @CurrPackageId
    FETCH NEXT FROM c2 INTO @CurrPackageId
  END
  SET @NoReceivedPackages = SUBSTRING(@NoReceivedPackages, 2, LEN(@NoReceivedPackages))
  UPDATE #t1 SET NoReceivedPackageIdList = @NoReceivedPackages WHERE BillTrackId = @CurrBillTrackId;

  DROP TABLE #t2
  CLOSE c2
  DEALLOCATE c2 
  FETCH NEXT FROM c1 INTO @CurrBillTrackId
 END
 CLOSE c1
 DEALLOCATE c1
 
 SELECT * FROM #t1
 DROP TABLE #t1
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.