T-SQL游标的使用范例


--定义变量
declare @i INT,@j int
declare @Propertyid CHAR(50)
declare @Title CHAR(50)
declare @RoomPic1 CHAR(50)
declare @RoomPic2 CHAR(50)
declare @RoomPic3 CHAR(50)
declare @HouseTypeShow1 CHAR(50)
declare @HouseTypeShow2 CHAR(50)
declare @HouseTypeShow3 CHAR(50)
declare @ComunityShow1 CHAR(50)
declare @ComunityShow2 CHAR(50)
declare @ComunityShow3 CHAR(50)
declare @VidCity CHAR(50)
declare @str char(50)
--定义游标把PropertyAds 的值赋给mycursor 游标
declare mycursor cursor for select PropertyId,Title,RoomPic1,RoomPic2,RoomPic3,HouseTypeShow1,HouseTypeShow2,HouseTypeShow3,ComunityShow1,ComunityShow2,ComunityShow3,VidCity from PropertyAds where Title is not null 
--declare mycursor cursor for select img,UseAs from EstateImgs
open mycursor --打开游标
select @i=count(*) from PropertyAds where Title is not null  --得到记录数

-- fetch next from mycursor INTO 将游标中值赋给你定义的变量

fetch next from mycursor INTO @Propertyid,@Title,@RoomPic1,@RoomPic2,@RoomPic3,@HouseTypeShow1,@HouseTypeShow2,@HouseTypeShow3,@ComunityShow1,@ComunityShow2,@ComunityShow3,@VidCity

--开始循环@@fetch_status=0

while @@fetch_status=0 and @i>1
----esnt 户ehxt--小区ehjt
BEGIN
    set @i=@i-1 
    --里面是你想要进行字段的判断和一系列的操作
if(LEFT(@RoomPic1,4)='2011')
begin
----PRINT @Propertyid+' '+@Title+' '+@RoomPic1+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
----set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@RoomPic1,'esnt',getdate(),null,@VidCity)
end 
if(LEFT(@RoomPic2,4)='2011')
begin
----PRINT @Propertyid+' '+@Title+' '+@RoomPic2+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
insert into PropertyImgs values(@Propertyid,@Title,@RoomPic2,'esnt',getdate(),null,@VidCity)
-- --set @j=@j+1
end 
if(LEFT(@RoomPic3,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@RoomPic3+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
insert into PropertyImgs values(@Propertyid,@Title,@RoomPic1,'esnt',getdate(),null,@VidCity)
--set @j=@j+1
end 
if(LEFT(@HouseTypeShow1,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@HouseTypeShow1+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
insert into PropertyImgs values(@Propertyid,@Title,@HouseTypeShow1,'ehxt',getdate(),null,@VidCity)
--set @j=@j+1
end 
if(LEFT(@HouseTypeShow2,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@HouseTypeShow2+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
--set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@HouseTypeShow2,'ehxt',getdate(),null,@VidCity)
end 
if(LEFT(@HouseTypeShow3,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@HouseTypeShow3+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
--set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@RoomPic1,'ehxt',getdate(),null,@VidCity)
end 
if(LEFT(@ComunityShow1,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@ComunityShow1+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
--set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@ComunityShow1,'ehjt',getdate(),null,@VidCity)
end 
if(LEFT(@ComunityShow2,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@ComunityShow2+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
--set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@ComunityShow2,'ehjt',getdate(),null,@VidCity)
end 
if(LEFT(@ComunityShow3,4)='2011')
begin
--PRINT @Propertyid+' '+@Title+' '+@ComunityShow3+' '+'esnt'+convert(varchar,getdate(),108) +' '+@VidCity
--set @j=@j+1
insert into PropertyImgs values(@Propertyid,@Title,@ComunityShow3,'ehjt',getdate(),null,@VidCity)
end 
    
PRINT @RoomPic1 + ' ' + @RoomPic2+ '  '+@str

print @j

--这下面一句必须要有的也下最重要的一句重新赋值

fetch next from mycursor INTO @Propertyid,@Title,@RoomPic1,@RoomPic2,@RoomPic3,@HouseTypeShow1,@HouseTypeShow2,@HouseTypeShow3,@ComunityShow1,@ComunityShow2,@ComunityShow3,@VidCity
END
close mycursor --关闭游标
deallocate mycursor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值