简体   繁体   中英

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.

According to business logic (client's requirement) some of the records are saved in tbCompanyAgent as shown in the first table.

Now if we remove data from tbLicence on the basis of LicenceType (Group Id) then all the corresponding data should be removed from 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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