Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
is it possible that we can use UPDATE And DELETE SQL Statements with left join query ?
Posted
Comments
Why you need a left join?

Yes it is possible...

Please check the below


C#
update Table_1
set columnTable_1 = @parameter_1
from Table_1 t1
inner join Table_2 t2
on t1.columnTable_1 = t2.columnTable_2
where t2.columnTable_1 = @parameter_2 

delete from Table_1
from Table_1 t1
inner join Table_2 t2
on t1.columnTable_1 = t2.columnTable_2
where t2.columnTable_2 = @parameter_1




Sample code......

SQL
create type abcd as table
(
	id int,
	salary int
)
create type nameAbcd as table
(
	id int,
	name varchar(3)
)

go
--drop type nameAbcd

declare @p abcd
insert into @p values(1, 100)
insert into @p values(2, 5000)
declare @q nameAbcd
insert into @q values(1, 'abc')
insert into @q values(2, 'xyz')


select * from @p a inner join @q b on a.id = b.id

update @p
set salary = 59
from @p a
inner join @q b
on a.id = b.id
where b.name = 'xyz'


select * from @p a inner join @q b on a.id = b.id

delete from @p
from @p a
inner join @q b
on a.id = b.id
where b.name = 'abc'


select * from @p a inner join @q b on a.id = b.id
 
Share this answer
 
Comments
VishnuAgarwal 13-Dec-15 16:24pm    
Thank you :)
Yes...
As both DELETE[^] and UPDATE[^] may have a FROM[^] clause, and any FROM clause can have JOIN in it - the answer is yes...
To get more defined answer, you should ask with more details...
 
Share this answer
 

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