•  

sql循环更新表字段值

2017-12-01

sql循环更新表字段值

 CREATE TABLE #t 
(NAME VARCHAR(20),
sort INT ,
isoncall INT 
)

INSERT INTO #t VALUES ('A',1,0)
INSERT INTO #t VALUES ('B',2,0)
INSERT INTO #t VALUES ('C',3,0)
INSERT INTO #t VALUES ('D',4,0)
INSERT INTO #t VALUES ('E',5,1)
INSERT INTO #t VALUES ('D',6,0)

DECLARE @i INT =(SELECT sort FROM #t WHERE isoncall=1)
IF  (@i<6) 
BEGIN 
 UPDATE #t SET isoncall=1 WHERE sort=@i+1
 UPDATE #t SET isoncall=0 WHERE sort=@i
 SET @i=@i+1
END 
ELSE 
BEGIN 
UPDATE #t SET isoncall=1 WHERE sort=1
UPDATE #t SET isoncall=0 WHERE sort=@i
SET @i=1
end
SELECT * FROM #t
DROP TABLE #T


申明:本文受法律保护,未经作者同意不得用于商业用途,如转载请说明出处!
评论