Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DECLARE @intFlag INT
SET @intFlag = 1
UPDATE #temp_patrons
		set
			 @intFlag = [order] = @intFlag + 1
			 IF @intFlag = 3
			 BEGIN
			   SET @intFlag = 1
             END


What I have tried:

i tried using loop or cases but nothing happened.
The output will be like this.
EX.
| order |
|   1   |
|   2   | 
|   3   |
|   1   |
|   2   |
|   3   |
Posted
Updated 17-Feb-19 20:16pm
v3

Can you do something like below?

1. Increment the flag
2. If flag = 3, reset to 1
3. Update operation

SQL
SET @intFlag = @intFlag + 1

	IF (@intFlag = 3)
		SET @intFlag = 1
	
	UPDATE #temp_patrons
		SET [order] = @intFlag
 
Share this answer
 
Comments
CaptainChizni 17-Feb-19 23:15pm    
The rows are all 1. What i am going to do is increment the numbers by 1 then if it reaches 3 the increment will reset
EX.
| order |
| 1 |
| 2 |
| 3 |
| 1 |
| 2 |
| 3 |
Bryian Tan 17-Feb-19 23:40pm    
Not clear what your query suppose to do, but I'm assuming there is a loop? I'll use Insert as an example because I'm sure there are more into it with the update statement.

DECLARE @test TABLE ([order] INT)

DECLARE @intFlag INT, @loopCount INT
SELECT @intFlag = 0, @loopCount = 0

WHILE (@loopCount < 6)
BEGIN
	SET @intFlag = @intFlag + 1

		IF (@intFlag > 3)
			SET @intFlag = 1
	
		INSERT INTO @test
			SELECT @intFlag

		SET @loopCount = @loopCount + 1
END


SELECT * FROM @test


Output:
order
1
2
3
1
2
3
No loops, no IF required:
SQL
UPDATE x SET x.[Order] = x.NewOrder
FROM (SELECT [Order], ((ROW_NUMBER() OVER(ORDER BY myOrderingColumnName) - 1) % 3) + 1 AS NewOrder 
      FROM MyTable) x;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900