• 嵌套游标以生成复杂的报表


    --以下示例显示如何嵌套游标以生成复杂的报表。为每个供应商声明内部游标
    SET NOCOUNT ON

    DECLARE @vendor_id int, @vendor_name nvarchar(50),
    @message varchar(80), @product nvarchar(50)

    PRINT '-------- Vendor Products Report --------'

    DECLARE vendor_cursor CURSOR FOR
    SELECT VendorID, Name
    FROM Purchasing.Vendor
    WHERE PreferredVendorStatus = 1
    ORDER BY VendorID

    OPEN vendor_cursor

    FETCH NEXT FROM vendor_cursor
    INTO @vendor_id, @vendor_name

    WHILE @@FETCH_STATUS = 0
    BEGIN
    PRINT ' '
    SELECT @message = '----- Products From Vendor: ' +
    @vendor_name

    PRINT @message

    -- Declare an inner cursor based  
    -- on vendor_id from the outer cursor.

    DECLARE product_cursor CURSOR FOR
    SELECT v.Name
    FROM Purchasing.ProductVendor pv, Production.Product v
    WHERE pv.ProductID = v.ProductID AND
    pv.VendorID = @vendor_id-- Variable value from the outer cursor

    OPEN product_cursor
    FETCH NEXT FROM product_cursor INTO @product

    IF @@FETCH_STATUS <> 0
    PRINT '         <<None>>'    

    WHILE @@FETCH_STATUS = 0
    BEGIN

    SELECT @message = '         ' + @product
    PRINT @message
    FETCH NEXT FROM product_cursor INTO @product

    END

    CLOSE product_cursor
    DEALLOCATE product_cursor

    -- Get the next vendor.
    FETCH NEXT FROM vendor_cursor
    INTO @vendor_id, @vendor_name
    END
    CLOSE vendor_cursor
    DEALLOCATE vendor_cursor
     

  • 相关阅读:
    %zsy %lqs 随感
    polynomial&generating function学习笔记
    PKUWC2020自闭记
    考前最后的感叹:CSP2019 Bless All! & AFO
    AFO
    NOI2019 Fe
    [十二省联考2019]骗分过样例 luoguP5285 loj#3050
    python异常处理,草稿
    python操作excel
    python网络编程(requests)
  • 原文地址:https://www.cnblogs.com/huyong/p/2685681.html
Copyright © 2020-2023  润新知