Link to home
Start Free TrialLog in
Avatar of aneilg
aneilgFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL Update

i am using a slowly changing dimension to update my table. but i need to incluse a few checks.
so after the table has been updated i am using another sp to do the checks.

what i need to do is, if the value in th ecode is NULL then use the previous code.

rowid   datecreated    validfrom   validto   iscurrent   businesskey   code      recdel
1          18/09               18/09           17/09    0                 C3                   CW        NO
2          18/09               18/09           NULL     1                 C3                   NULL    NO

Thanks.
SOLUTION
Avatar of Saurabh Bhadauria
Saurabh Bhadauria
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

sorry, but i dont understand how this wil work.

update [WARD_SCD2_HISTORY]
set
 = 
case when @update_value is null 
then [Code] 
else @update_value                               
end 

Open in new window

the code chould be any value, so i cannot use the same value
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

all its done is set all the values in the code column to null.

i want to use previous value if null
Avatar of aneilg

ASKER

i've been looking at CTE and coalesce
SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

still not what i am looking for.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

sorry to be a pain.
Incorrect syntax near the keyword 'from'.
Avatar of aneilg

ASKER

this works for reference.


UPDATE  TABLE  
SET     Code = c2.Code  
OUTPUT  Inserted.*  

FROM    TABLEc  
        INNER JOIN (  
          SELECT  c.[DateCreated]
                  , c.[BusinessKey]
                  , Code = COALESCE(c.Code, cprevious.Code)
          FROM    TABLEc
                  INNER JOIN (
                    SELECT  c.[DateCreated]
                            , c.[BusinessKey]
                            , MaxDate = MAX(cdates.[DateCreated])
                    FROM    TABLEc
                            LEFT OUTER JOIN (
                              SELECT  [DateCreated]
                                      , [BusinessKey]
                              FROM    TABLE
                            ) cdates ON cdates.[DateCreated] < c.[DateCreated] AND cdates.[BusinessKey] = c.[BusinessKey]
                    GROUP BY
                            c.[DateCreated], c.[BusinessKey]
                  ) cmax ON cmax.[DateCreated] = c.[DateCreated] AND cmax.[BusinessKey] = c.[BusinessKey]                  
                  LEFT OUTER JOIN TABLE cprevious ON cprevious.[DateCreated] = cmax.MaxDate AND cprevious.[BusinessKey] = cmax.[BusinessKey]
        ) c2 ON c2.[DateCreated] = c.[DateCreated]  
                AND c2.[BusinessKey] = c.[BusinessKey]
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aneilg

ASKER

partly answered.