Steps:
1: First define a temporary table, put the required data into the temporary table, if the data is not contiguous, then define a self-growth key in the temporary table
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
Inch
(
' EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A ',
' 667557e2-d5a9-43fe-a18a-21dd68e19207 '
)
and (cardgiftmoney>0 or cardmoney>0)
2: Define two fields of type int, one to determine the number of rows currently running, and one to hold the number of rows in the staging table that need to be traversed
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
Inch
(
' EE034CB5-EF4B-4E5B-928D-2D3F733D5B4A ',
' 667557e2-d5a9-43fe-a18a-21dd68e19207 '
)
and (cardgiftmoney>0 or cardmoney>0))
3: Traverse the data according to the two fields defined in the self-growth key and 2
DECLARE @Id INT
DECLARE @ShopCode uniqueidentifier
DECLARE @CustCode uniqueidentifier
Declare@cardmoney DECIMAL
Declare@cardgiftmoney DECIMAL
Declare@createtime DATETIME
SET @Id =1
while (@rowcount <[email protected])
BEGIN
Select TOP 1 @ShopCode =shopcode, @CustCode =custcode, @CardMoney =cardmoney, @CardGiftMoney =cardgiftmoney, @CreateTime = Createtime from @temp where [email protected]
Update HL10.dbo.t_CustVCard Set [email protected],[email protected],
[email protected] where [email protected] and [email protected]
SET @[email protected]+1
SET @[email protected]+1
END
4: The total code is as follows
DECLARE @temp Table (
Id INT IDENTITY (1, 1),
Shopcode uniqueidentifier,
Custcode uniqueidentifier,
Card Money DECIMAL,
Cardgiftmoney decimal,
createtime DATETIME
)
inserts 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.db O.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 <[email protected])
BEGIN
Select TOP 1 @ Shopcode=shopcode, @CustCode =custcode, @CardMoney =cardmoney, @CardGiftMoney =cardgiftmoney, @CreateTime =createtime From @temp where [email protected]
Update HL10.dbo.t_CustVCard Set [email protected],[email protected],
[email protected] where [email protected] and [email protected]
SET @[email protected]+1
SET @[email protected]+1
END
Data traversal in SQL tables