I have following table structure.
| A | B | C | D | E |
+------+-----+-----+--------+---+
| 1000 | 5 | 2 | 2018-A | |
| 1001 | 15 | 63 | 2018-A | |
| 1002 | 25 | 124 | 2018-A | |
| 1003 | 35 | 185 | 2018-A | |
| 1004 | 45 | 246 | 2018-A | |
| 1005 | 55 | 307 | 2018-A | |
| 1000 | 65 | 368 | 2018-B | |
| 1001 | 75 | 429 | 2018-B | |
| 1000 | 85 | 490 | 2018-C | |
| 1001 | 95 | 551 | 2018-C | |
| 1002 | 105 | 612 | 2018-C | |
| 1003 | 115 | 673 | 2018-C | |
| 1004 | 125 | 734 | 2018-C | |
I need to update E column by multiplying B*C. In my stored procedure I tried following query, but it only updated the last row:
SELECT @A = a,
@Ans = b * c
FROM tbln
WHERE d = '2018-A'
UPDATE tbln
SET e = @ANS
WHERE d = '2018-A'
AND a = @A
3条答案
按热度按时间mtb9vblg1#
Problem is your query
only initialise your variables @A and @Ans with one row infos. And only that one is updated afterwards To update all, just use:
z4iuyo4d2#
In the first statement you are setting
@A = A
. Given that column A is the primary key of your table it is uniquely identifying each row. When the first SELECT statement is done executing@A = 1004
.Then your UPDATE statement executes and is filtering for A = @A which is A = 1004.
If you want to update column E for all rows in the sample then you need to remove your WHERE clause.
dgenwo3n3#
SELECT @A = a,
Will only have one value at the end and so you will update only one row
What you need to do is followinf to update all rows with 2018-A
fiddle