Re: How do I avoid this corelated subquery?
From: Michael D (sorengi_at_yahoo.com)
Date: 05/13/04
- Next message: Aaron Bertrand - MVP: "Re: Help me convince the dev manager. Please."
- Previous message: Aaron Bertrand - MVP: "Re: a technical question about guids"
- In reply to: John Gilson: "Re: How do I avoid this corelated subquery?"
- Next in thread: Erland Sommarskog: "Re: How do I avoid this corelated subquery?"
- Reply: Erland Sommarskog: "Re: How do I avoid this corelated subquery?"
- Messages sorted by: [ date ] [ thread ]
Date: 13 May 2004 07:55:45 -0700
Why not use a function ?
CREATE FUNCTION sumLimitAmt (@Amt_Num int)
RETURNS int
AS
BEGIN
DECLARE @RtnValue int
if(@Amt_Num = 1)
SELECT @RtnValue = SUM(l.amt1)
FROM Limits l
INNER JOIN TempLimits tl
ON (
l.relid = tl.relid AND
l.prodid = tl.prodid
)
WHERE tl.maxdays <= l.days
if(@Amt_Num = 2)
SELECT @RtnValue = SUM(l.amt2)
FROM Limits l
INNER JOIN TempLimits tl
ON (
l.relid = tl.relid AND
l.prodid = tl.prodid
)
WHERE tl.maxdays <= l.days
RETURN(@RtnValue)
END
go
UPDATE Limits
SET amt1 = amt1 + dbo.sumLimitAmt(1),
amt2 = amt2 + dbo.sumLimitAmt(2)
--select dbo.sumLimitAmt(1)
--select dbo.sumLimitAmt(2)
Michael D.
- Next message: Aaron Bertrand - MVP: "Re: Help me convince the dev manager. Please."
- Previous message: Aaron Bertrand - MVP: "Re: a technical question about guids"
- In reply to: John Gilson: "Re: How do I avoid this corelated subquery?"
- Next in thread: Erland Sommarskog: "Re: How do I avoid this corelated subquery?"
- Reply: Erland Sommarskog: "Re: How do I avoid this corelated subquery?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|