Data traversal in SQL tables

Source: Internet
Author: User
Tags rowcount

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.