当前位置:Gxlcms > 数据库问题 > sql表中数据遍历

sql表中数据遍历

时间:2021-07-01 10:21:17 帮助过:10人阅读

 

DECLARE @temp table(
Id INT IDENTITY(1, 1) ,
ShopCode UNIQUEIDENTIFIER,
CustCode UNIQUEIDENTIFIER,
CardMoney DECIMAL,
CardGiftMoney DECIMAL,
CreateTime DATETIME
)
INSERT INTO @temp 
select ShopCode,CustCode,CardMoney,CardGiftMoney,CreateTime from HL11.dbo.t_ShopSerLog 
where CreateTime>=‘2017-07-01‘ and CreateTime<‘2017-07-02‘ and [Status]=1 and DeleFlag=1 
and ShopCode
in
(
‘EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A‘,
‘667557E2-D5A9-43FE-A18A-21DD68E19207‘
)
and (CardGiftMoney>0 or CardMoney>0)

 

2:定义两个int类型的字段,一个用于判断当前运行的行数,一个用于存放临时表中需要遍历的总行数

 

DECLARE @rowcount INT 
DECLARE @allcount INT 
SET @rowcount=1
SET @allcount=(select COUNT(1) from HL11.dbo.t_ShopSerLog 
where CreateTime>=‘2017-07-01‘ and CreateTime<‘2017-07-02‘ and [Status]=1 and DeleFlag=1 
and ShopCode
in
(
‘EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A‘,
‘667557E2-D5A9-43FE-A18A-21DD68E19207‘
)
and (CardGiftMoney>0 or CardMoney>0))

 

 

3:根据自增长键以及2里面定义的两个字段,对数据进行遍历处理

DECLARE @Id INT 
DECLARE @ShopCode UNIQUEIDENTIFIER
DECLARE @CustCode UNIQUEIDENTIFIER
DECLARE @CardMoney DECIMAL
DECLARE @CardGiftMoney DECIMAL
DECLARE @CreateTime DATETIME
SET @Id=1
WHILE(@rowcount<=@allcount)
BEGIN 
select TOP 1 @ShopCode=ShopCode,@CustCode=CustCode,@CardMoney=CardMoney,@CardGiftMoney=CardGiftMoney,@CreateTime=CreateTime from @temp where Id=@Id

update HL10.dbo.t_CustVCard set PaidAmount=PaidAmount-@CardMoney,GiftAmount=GiftAmount-@CardGiftMoney,
LastUsedTime=@CreateTime where ShopCode=@ShopCode and CustUniqCode=@CustCode

SET @Id=@Id+1
SET @rowcount=@rowcount+1

END

 

4:总的代码如下

DECLARE @temp table(
Id INT IDENTITY(1, 1) ,
ShopCode UNIQUEIDENTIFIER,
CustCode UNIQUEIDENTIFIER,
CardMoney DECIMAL,
CardGiftMoney DECIMAL,
CreateTime DATETIME
)
INSERT INTO @temp
select ShopCode,CustCode,CardMoney,CardGiftMoney,CreateTime from HL11.dbo.t_ShopSerLog
where CreateTime>=‘2017-07-01‘ and CreateTime<‘2017-07-02‘ and [Status]=1 and DeleFlag=1
and ShopCode
in
(
‘EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A‘,
‘667557E2-D5A9-43FE-A18A-21DD68E19207‘
)
and (CardGiftMoney>0 or CardMoney>0)


DECLARE @rowcount INT
DECLARE @allcount INT
SET @rowcount=1
SET @allcount=(select COUNT(1) from HL11.dbo.t_ShopSerLog
where CreateTime>=‘2017-07-01‘ and CreateTime<‘2017-07-02‘ and [Status]=1 and DeleFlag=1
and ShopCode
in
(
‘EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A‘,
‘667557E2-D5A9-43FE-A18A-21DD68E19207‘
)
and (CardGiftMoney>0 or CardMoney>0))
DECLARE @Id INT
DECLARE @ShopCode UNIQUEIDENTIFIER
DECLARE @CustCode UNIQUEIDENTIFIER
DECLARE @CardMoney DECIMAL
DECLARE @CardGiftMoney DECIMAL
DECLARE @CreateTime DATETIME
SET @Id=1
WHILE(@rowcount<=@allcount)
BEGIN
select TOP 1 @ShopCode=ShopCode,@CustCode=CustCode,@CardMoney=CardMoney,@CardGiftMoney=CardGiftMoney,@CreateTime=CreateTime from @temp where Id=@Id

update HL10.dbo.t_CustVCard set PaidAmount=PaidAmount-@CardMoney,GiftAmount=GiftAmount-@CardGiftMoney,
LastUsedTime=@CreateTime where ShopCode=@ShopCode and CustUniqCode=@CustCode

SET @Id=@Id+1
SET @rowcount=@rowcount+1

END

sql表中数据遍历

标签:sql表中   while   entity   tun   set   eid   money   style   rdm   

人气教程排行