Sql Server 游标操作的例子,使用sql server 游标循环处理数据

 
GO
-- =============================================
-- Author:        zqt
-- Create date: 2011-11-25
 
-- =============================================
create proc [dbo].[proc_get_product]
  @customerID int 
as

--声明一个变量  
 declare @productid   int; 
 declare @productname nvarchar(200) 
 
--申明一个游标
DECLARE C_userOrder CURSOR    
    FOR  
    select p.productid,p.productname  
    from products p where p.customerID = @customerID  
--打开一个游标    
OPEN C_userOrder 
--循环一个游标
    fetch next from C_userOrder into @productid, @productname ;
WHILE @@FETCH_STATUS =0
    BEGIN 
    -- 这里写自己的循环内容 
           print @productname 
        
    END    

--关闭游标
CLOSE C_userOrder
--释放资源
DEALLOCATE C_userOrder

/* 测试
    exec proc_get_product 1
*/ 

猜你喜欢

转载自www.cnblogs.com/davies/p/10415912.html