Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Fill in the serial number in the specified field after SQL sorting

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Display the line number

If the data is not deleted, the primary key is consistent with the row number, but when some data is deleted, the row number is inconsistent with the primary key, so you need to query the line number with a new method. Before SQL Server2005, you need to use a temporary table, but in SQL Server2005, it is very convenient to use ROW_NUMBER ().

The following is a query statement, which means to sort the data by cid, and then fill in the new field rowNum with the sorted sequence number.

Select row_number () over (order by cid) as rowNum,* from t_gene

Query results:

two。 Use

The following is what this article needs to achieve, that is, after sorting, update the sorted sequence number to the specified field.

With ts as (select row_number () over (order by clccode) as rn,* from t_gene) update tsset cdkxh=rn

Running result:

Before sorting. At this point, you can see that the field cdkxh size is uneven.

Select * from t_gene

After sorting, update the sequence number of the temporary field rn to the field cdkxh

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report