Friday, March 30, 2012

Problems with using Variables in place of Nested Queires

Ok, I had a problem yesterday where the code was erroring out before running. Now it looks like I am presented with some logic errors that may be because of how I have implemented the variables.

Right now the below code runs, but it does not affect any codes "(0 row(s) affected)". I have been able to pull up over 500 rows that the code should affect. I think their may be a fundimental problem with the code below because if I substitute a specific number for the values of the variables, then I still produce the same results (no rows affected).

Ack please help!

DECLARE @.q4TotRebate int, @.sumRebate int, @.q4TotRed int, @.sumRedeemed int

Update tblTransactionSummary

Set TotalRebate =
CASE
WHEN ((@.q4TotRebate + @.sumRebate)-(@.q4TotRed + @.sumRedeemed)) < 0
THEN (TotalRebate - ((@.q4TotRebate + @.sumRebate)-(@.q4TotRed + @.sumRedeemed)))
WHEN ((@.q4TotRebate + @.sumRebate)-(@.q4TotRed + @.sumRedeemed)) > 0
THEN TotalRebate
END

From tblTransactionSummary as t
WHERE

@.q4TotRebate = ISNULL((Select n.TotalRebateAmount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4'),0)
AND
@.sumRebate = ISNULL(((SELECT SUM(b.TransactionAmount)
FROM tblTransactionDetail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionType = 'P'
and b.TransactionDate > '12/31/06'
AND b.TransactionDate < '01/06/07') * .01), 0)
AND
@.q4TotRed =ISNULL((Select SUM(g.QtrRebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1'),0)
AND
@.sumRedeemed = ISNULL((SELECT SUM(m.TransactionAmount)
FROM tblTransactionDetail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionType = 'R'
AND m.TransactionDate > '12/31/06'
AND m.TransactionDate < '01/06/07'),0)http://www.msdner.com/forum/thread591536.html

No comments:

Post a Comment