zoukankan      html  css  js  c++  java
  • SQL CURSOR

    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;
  • 相关阅读:
    Go语言入门
    简述cookies 和 session
    Linux inode 理解
    BZOJ 1012 最大数maxnumber
    BZOJ 1087 互不侵犯king
    CSS从大图中抠取小图完整教程(background-position应用)
    javascript中i++与++i
    脱离文档流分析
    在Windows上以zip压缩包方式安装mysql
    centos7 python2.7下安装paramiko模块
  • 原文地址:https://www.cnblogs.com/RR-ghost/p/5124215.html
Copyright © 2011-2022 走看看