首页 > 代码库 > 游标学习案例

游标学习案例

游标学习案例

  1 declare @GoodsID varchar(255);  2 declare @GoodsName varchar(255);  3 set @GoodsID =G050003 ;  4 set @GoodsName=美的空调;  5 select @GoodsID 商品编号,@GoodsName 商品名称  6   7   8 select * from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName  9  10 select top 10 * from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName 11  12 declare ItemItem  CURSOR 13 for select top 10 FNumber,FName,FModel from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName 14  15  16 ---练习1 17  18  19 --定义测试表 20 create table Test_Item( 21 ID int, 22 FNumber varchar(255), 23 FName varchar(255), 24 FModel varchar(255)); 25 GO 26 declare @rowNums int; 27 declare @FNumber varchar(255); 28 declare @FName  varchar(255); 29 declare @FModel varchar(255); 30 declare @i int; 31 declare Item_CURSOR  CURSOR 32 for select top 10 FNumber,FName,FModel from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName; 33 open Item_CURSOR 34     set @i=1; 35     --while(@@FETCH_STATUS=0) 36     while(1=1) 37     begin 38         FETCH NEXT FROM Item_CURSOR into @FNumber,@FName,@FModel; 39         --print @@CURSOR_ROWS; 40         print 开始; 41         print @i; 42         if(@i<=10) 43            insert into Test_Item values(@i,@FNumber,@FName,@FModel) 44         else 45            break; 46         print 结束; 47         set @i=@i+1; 48         select @rowNums=Count(FName) from Test_Item; 49     end 50 close Item_CURSOR 51  52  53  54 DEALLOCATE  Item_CURSOR; 55  56 drop table Test_Item; 57  58 select * from Test_Item 59  60  61  62 ---练习2 63  64  65 --定义测试表 66 create table Test_Item( 67 ID int, 68 FNumber varchar(255), 69 FName varchar(255), 70 FModel varchar(255)); 71 GO 72  73  74  75 declare @FNumber varchar(255); 76 declare @FName  varchar(255); 77 declare @FModel varchar(255); 78 declare @ID int; 79 declare Item_CURSOR  CURSOR 80 for select top 10 FNumber,FName,FModel from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName; 81 open Item_CURSOR 82     set @ID=0; 83     FETCH NEXT FROM Item_CURSOR into @FNumber,@FName,@FModel; 84     while(@@FETCH_STATUS=0) 85     begin 86         set @ID=@ID+1; 87         print 开始; 88         insert into Test_Item values(@ID,@FNumber,@FName,@FModel) 89         print 结束; 90         FETCH NEXT FROM Item_CURSOR into @FNumber,@FName,@FModel; 91                92     end 93 close Item_CURSOR 94  95  96  97 DEALLOCATE  Item_CURSOR; 98  99 drop table Test_Item;100 101 102 select top 10 * from t_ICItem where FNumber like 04.01.% and len(FNUmber)=9 order by FName103 104 select * from Test_Item

 

游标学习案例