in reply to OT: SQL problem

A couple of comments:

1) there is no guarantee that the update is acting on a single record. What should happen if your subquery returns more than one record?

2)in the second update you set the field to a constant based on a table join. The subquery is expensive in terms of time and probably unnecessary

It might be better to calculate the result first then update in one step. This might work
DECLARE @result float -- if you are certain there is only one record per condition (primary +key constraints or something -- you can omit this statement and the IF..ELSE block /* SET @result = (SELECT COUNT(*) FROM Calculation D INNER JOIN Lookup_table L ON D.SUM_xi = L.SUM_xi WHERE (D.SUM_xi < 13) AND (D.Key_m = @Count_me)); */ -- try this instead SET @result = (SELECT COUNT(*) FROM Calculation D WHERE (D.SUM_xi < 13) AND (D.Key_m = @Count_me)); IF(@result = 1) begin SET @result = (SELECT D.SUM_wixi + SQRT(D.SUM_wi2xi / D.SUM_xi) * +(L.c - L.SUM_xi) FROM Calculation D INNER JOIN Lookup_table L ON D.SUM_xi = L.SUM_xi WHERE (D.SUM_xi < 13) AND (D.Key_m = @Count_me)); UPDATE Calculation SET Lower_95 = @result, Lower_95_Calc_method = 'UPDATE' WHERE SUM_xi < 13 AND Key_m = @Count_me; SET @Count_me = @Count_me + 1; end ELSE begin -- do something here to indicate multiple or zero records returned end

Update: I just noticed the WHERE condition doesn't require Lookup_table. You could simplify the query to a select on Calculation only. It will save you some time on the lookup. You could even incorporate the lookup back into your original query (but then you would lose some error checking)

PJ
use strict; use warnings; use diagnostics;

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.