Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Set value of a field in TABLE1 to NULL - where the join between TABLE1 and TABLE2 results in NULL values

$
0
0

I am working on a database where I am not able to change any of the table names / attributes / joins

I have had a look on the forum on lots of similar issues, but none really hit the nail on the head, even if they have gotten me a little closer, hopefully someone will be able to make sense of my post

I have 2 tables SSRFACC and SSRFADD

SSRFACC contains a field called ADD_CODE with should only contain values in SSRFADD.ADD_CODE - but the table does not have actual joins so over time some entries have appeared in SSRFACC.ADD_CODE which do not appear in SSRFADD.ADD_CODE

I have created the code that identifies where the resultant join returns NULL values - this works okay and is shown below - but it's the next step - trying to set the"spurious" values that have appeared in SSRFACC.ADD_CODE to NULL - my inital attempt is shown after this select code

--this is the select code that works as expected

select * from SSRFACC
left outer join SSRFADD
on SSRFACC.ADD_CODE = SSRFADD.ADD_CODE
and SSRFACC.SUN_DB = SSRFADD.SUN_DB
where SSRFADD.ADD_CODE is null
and SSRFACC.SUN_DB = 'AWU'
and ACCNT_TYPE in ('C' , 'D')
and SSRFACC.ADD_CODE not like ''
order by ACCNT_CODE

--this is the end of the select code

The UPDATE code is below: (I have created copies of the TWO tables and renamed them DEV_SSRFADD and DEV_SSRFACC to avoid messing with the data.

I am getting an error message:

Msg 8156, Level 16, State 1, Line 1
The column 'SUN_DB' was specified multiple times for 'DEV_SSRFADD'.

--this is the start of the update code

update DEV_SSRFACC

set DEV_SSRFACC.ADD_CODE = DEV_SSRFADD.ADD_CODE --(here I want this in essence set to NULL)

From
( select * from DEV_SSRFACC
left outer join DEV_SSRFADD
on DEV_SSRFACC.ADD_CODE = DEV_SSRFADD.ADD_CODE
and DEV_SSRFACC.SUN_DB = DEV_SSRFADD.SUN_DB
where DEV_SSRFADD.ADD_CODE is null
and DEV_SSRFACC.SUN_DB = 'AWU'
and ACCNT_TYPE in ('C' , 'D', 'T')
and DEV_SSRFACC.ADD_CODE not like ''
 ) AS DEV_SSRFADD

where DEV_SSRFACC.ADD_CODE = DEV_SSRFADD.ADD_CODE AND DEV_SSRFACC.SUN_DB = DEV_SSRFADD.SUN_DB

--this is the end of the update code

--(I dont know whether the last where clause is surpurflous or not)


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>