简体   繁体   English

从相应表中删除记录

[英]Delete records from corresponding table

I have two tables in the database as shown in the screenshot... 我在数据库中有两个表,如屏幕截图所示... 在此处输入图片说明

Please note that LicenceType in the second table is behaving as GroupId in order to classify group of records. 请注意,第二张表中的LicenceType表现为GroupId,以便对记录组进行分类。

According to business logic (client's requirement) some of the records are saved in tbCompanyAgent as shown in the first table. 根据业务逻辑(客户的要求),某些记录将保存在tbCompanyAgent中,如第一张表所示。

Now if we remove data from tbLicence on the basis of LicenceType (Group Id) then all the corresponding data should be removed from tbCompanyAghent. 现在,如果我们根据LicenceType(组ID)从tbLicence中删除数据,那么所有相应的数据都应从tbCompanyAghent中删除。

For doing so, I have written below query 为此,我在下面编写了查询

DELETE FROM tbLicence WHERE LicenceType = @LicenceType

DELETE FROM tbCompanyAgent

SELECT * FROM tbCompanyAgent INNER JOIN tbLicence ON tbCompanyAgent.LicenceNumber = tbLicence.LicenceNumber
    OR tbCompanyAgent.LicenceNumber = tbLicence.StateIssuedLicenseNumber
    OR tbLicence.LicenceType = @LicenceType

but it doesn't do as required. 但它没有按要求执行。 Please help !!! 请帮忙 !!!

      Delete from tbCompanyAgent  
      where LicenceNumber in (select distinct LicenceNumber 
      from tbLicence where LicenceType= @LicenceType)          

      Delete from tbLicence where LicenceType = @LicenceType 

To Avoid the Sub Query, Can you try this 为了避免子查询,您可以尝试一下吗

delete a from tblCompanyAgent a 
inner join tblLicence b on a.LicenceNumber = b.LicenceNumber 
where b.LicenceType = @licenceType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM