由于疏忽,数据库的排序字段,成了0,导致,排序出现错误

下面这个,单独执行,给再加上了排序

 1 declare @testPagerId int
 2 set @testPagerId=12
 3 
 4 IF OBJECT_ID('dbo.#GroupQuestion','U') IS NOT NULL DROP TABLE dbo.#GroupQuestion;
 5 
 6 SELECT *
 7 INTO dbo.#GroupQuestion
 8 FROM  GroupQuestion where testPagerId=@testPagerId
 9 order by id
10 
11 -- 声明变量
12 DECLARE
13     @id INT,
14     @rank int
15 set @rank=0
16     
17 WHILE EXISTS(SELECT id FROM dbo.#GroupQuestion)
18 BEGIN
19     -- 也可以使用top 1
20     set @rank=@rank+1
21     SELECT top 1 @id=id FROM dbo.#GroupQuestion;
22     UPDATE GroupQuestion SET sort=@rank WHERE id=@id;    
23     DELETE FROM dbo.#GroupQuestion WHERE id=@id;
24 END
25 select * from dbo.#GroupQuestion
26 DROP TABLE dbo.#GroupQuestion;

 

相关文章:

  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
  • 2021-06-02
猜你喜欢
  • 2021-10-17
  • 2021-12-12
  • 2021-07-24
  • 2021-11-27
  • 2021-10-05
  • 2021-11-12
  • 2021-11-01
相关资源
相似解决方案