我需要编写一个具有以下要求的SQL ..
现在,我需要删除表A,其中AA = 100 ......但这只应在BB = 7列时发生。
任何人都可以帮我写上面的SQL。
答案 0 :(得分:2)
如果ba中存在一行或多行,其中bb等于7,则下面的语句将删除aa等于100的所有行。
delete from a where aa = 100 and exists (select * from b where bb = 7);
答案 1 :(得分:0)
如果您愿意,请查看此项。
create table A (AA int)
create table B (BB int)
insert into A values (100)
insert into B values (1)
--Invalid Case
if exists (select 1 from B where BB = 7)
begin
delete from A where aa = 100
if @@rowcount > 0
begin
print 'BB in B has value euqal to 7'
end
end
else
begin
print 'BB in B not equals to 7'
end
--Valid Case
update B set BB = 7
if exists (select 1 from B where BB = 7)
begin
delete from A where aa = 100
if @@rowcount > 0
begin
print 'BB in B has value euqal to 7'
end
end
else
begin
print 'BB in B not equals to 7 and Data Deleted from A'
end