Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to delete from table in case of code type have value ?

I have temp table #temppc

I need when codetype on temp table #tempPc not have null meaning have value

then

delete statement from table Parts.TradeCodes will be
SQL
DELETE TCC FROM #TempPC t INNER JOIN Parts.TradeCodes TCC ON t.ZPLID=TCC.ZPLID and t.CodeType=TCC.codeType

and if codetype on temp table #TempPc is null then delete statment from table Parts.TradeCodes will not have codetype

and it will be
SQL
DELETE TCC FROM #TempPC t INNER JOIN Parts.TradeCodes TCC ON t.ZPLID=TCC.ZPLID

so How to modify delete statment on query below that to delete based on two cases if codetype have value or not

What I have tried:

How to modify delete statment below to delete if code type have value or not
if code type have value then and t.codetype=tcc.codetype
if not
it will be as below
SQL
DELETE TCC FROM #TempPC t INNER JOIN Parts.TradeCodes TCC ON t.ZPLID=TCC.ZPLID

SQL
create table #TempPC 
(
[ID] INT IDENTITY,
ZPLID INT,
CodeType int

)

SQL
DELETE TCC FROM #TempPC t INNER JOIN Parts.TradeCodes TCC ON t.ZPLID=TCC.ZPLID 
Posted
Updated 27-Jan-20 20:54pm
v2

1 solution

If you want to mach NULL CodeType in both tables then this is the way to do it:
SQL
DELETE TCC FROM #TempPC t 
INNER JOIN Parts.TradeCodes TCC 
  ON t.ZPLID=TCC.ZPLID
  AND ((t.CodeType IS NULL AND TCC.codeType IS NULL) OR t.CodeType=TCC.codeType)
 
Share this answer
 
Comments
ahmed_sa 28-Jan-20 3:30am    
why AND TCC.codeType IS NULL
i SEE
t.CodeType IS NULL IS enough
Tomas Takac 28-Jan-20 7:48am    
Depends on you use case. If this works for you, then it's ok. My example matches the records having CodeType NULL on both sides.
Maciej Los 28-Jan-20 3:58am    
5ed!
ahmed_sa 28-Jan-20 4:16am    
this is correct or wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900