I have 2 tables. GTable (GId, GText) CTable (CId, GId, CText)
I would like to delete row from GTable when there are no rows in CTable that refer to GTable.
For example. If there are 2 rows in CTable with GId=4 no action.
But if there are 0 rows with GId=5 I would like to delete from GTable the row GId=5.
How is best to do this?
declare @GID int
set @GID = 3 -- Or your inputed value
delete from GTable where GId = @GID and not exists(select 1 from CTable where GId = @GID)
delete from GTable where GId = @GID and not exists(select 1 from CTable where GId = @GID)
--if you want to delete full table then use this command
delete from GTable where GId not in (select GID from CTable)