Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: OT: SQL problem

by periapt (Hermit)
on Jan 25, 2005 at 13:55 UTC ( #424866=note: print w/replies, xml ) Need Help??

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)

use strict; use warnings; use diagnostics;

Replies are listed 'Best First'.
Re^2: OT: SQL problem
by Anonymous Monk on Jan 25, 2005 at 14:36 UTC
    This bit of code works independently (as before) and with the SPROC (not before). Which is better. But it still stalls the SPROC. Strange.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://424866]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2017-06-29 13:37 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (667 votes). Check out past polls.