Hi Gang
I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have been able to adjust with simply SET statements or nested queries. To fix one field I need to either make a very complex nested query or find a way to set variables to queries. But first I have to check to confirm that each query is not null.
Please tell me if this is even possible. (or is there a way to initialize to a 0 value and then not subsitute if NULL?)
Here is an example of what I am trying to do:
DECLARE @q4TotRebate int, @sumRebate int, @q4TotRed int, @sumRedeemed int
SET @q4TotRebate = (Select n.TotalRebateAm ount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4')
SET @sumRebate = (SELECT SUM(b.Transacti onAmount)
FROM tblTransactionD etail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionTy pe = 'P'
and b.TransactionDa te > '12/31/06'
AND b.TransactionDa te < '01/06/07')
SET @q4TotRed = (Select SUM(g.QtrTotalR ebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1')
SET @sumRedeemed = (SELECT SUM(m.Transacti onAmount)
FROM tblTransactionD etail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionTy pe = 'R'
AND m.TransactionDa te > '12/31/06'
AND m.TransactionDa te < '01/06/07')
SET @q4TotRebate =
CASE
WHEN @q4TotRebate IS Null
THEN 0
WHEN @q4TotRebate IS NOT NULL
THEN @q4TotRebate
END
SET @sumRebate =
CASE
WHEN @sumRebate IS NULL
THEN 0
WHEN @sumRebate IS NOT NULL
THEN @sumRebate
END
SET @q4TotRed =
CASE
WHEN @q4TotRed is NULL
THEN 0
WHEN @q4TotRed IS NOT NULL
THEN @q4TotRed
END
SET @sumRedeemed =
CASE
WHEN @sumRedeemed IS NULL
THEN 0
WHEN @sumRedeemed IS NOT NULL
THEN @sumRedeemed
END
From tblTransactionS ummary as t
Update tblTransactionS ummary
Set TotalRebate =
CASE
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) < 0
THEN (TotalRebate - ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)))
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) > 0
THEN TotalRebate
END
I am working on fixing a corrupted database (A yearly proc did not handel the conversion from year to year and now I have to fix the resulting lines in the DB). Most of my fixes I have been able to adjust with simply SET statements or nested queries. To fix one field I need to either make a very complex nested query or find a way to set variables to queries. But first I have to check to confirm that each query is not null.
Please tell me if this is even possible. (or is there a way to initialize to a 0 value and then not subsitute if NULL?)
Here is an example of what I am trying to do:
DECLARE @q4TotRebate int, @sumRebate int, @q4TotRed int, @sumRedeemed int
SET @q4TotRebate = (Select n.TotalRebateAm ount
FROM tblSummary as n
WHERE t.AccountNumber = n.AccountNumber
AND Quarter = '2006/Q4')
SET @sumRebate = (SELECT SUM(b.Transacti onAmount)
FROM tblTransactionD etail as b
WHERE t.AccountNumber = b.AccountNumber
AND b.TransactionTy pe = 'P'
and b.TransactionDa te > '12/31/06'
AND b.TransactionDa te < '01/06/07')
SET @q4TotRed = (Select SUM(g.QtrTotalR ebateRedeemed)
FROM tblSummary as g
WHERE g.AccountNumber = t.AccountNumber
And Quarter < '2007/Q1')
SET @sumRedeemed = (SELECT SUM(m.Transacti onAmount)
FROM tblTransactionD etail as m
WHERE t.AccountNumber = m.AccountNumber
AND m.TransactionTy pe = 'R'
AND m.TransactionDa te > '12/31/06'
AND m.TransactionDa te < '01/06/07')
SET @q4TotRebate =
CASE
WHEN @q4TotRebate IS Null
THEN 0
WHEN @q4TotRebate IS NOT NULL
THEN @q4TotRebate
END
SET @sumRebate =
CASE
WHEN @sumRebate IS NULL
THEN 0
WHEN @sumRebate IS NOT NULL
THEN @sumRebate
END
SET @q4TotRed =
CASE
WHEN @q4TotRed is NULL
THEN 0
WHEN @q4TotRed IS NOT NULL
THEN @q4TotRed
END
SET @sumRedeemed =
CASE
WHEN @sumRedeemed IS NULL
THEN 0
WHEN @sumRedeemed IS NOT NULL
THEN @sumRedeemed
END
From tblTransactionS ummary as t
Update tblTransactionS ummary
Set TotalRebate =
CASE
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) < 0
THEN (TotalRebate - ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)))
WHEN ((@q4TotRebate + @sumRebate)-(@q4TotRed + @sumRedeemed)) > 0
THEN TotalRebate
END
Comment