Delete from Child than from Parent table using SQL Query

Shabbir Daruwala 176 Reputation points
2021-09-30T11:30:42.66+00:00

Hi All,

How to delete from Child table and than from parent table with same filter, below is sql script but require refactoring

DELETE From tbl_AutoSizeDetail where AutoSizeId in (Select Id from  FROM tbl_AutoSize
                        WHERE 1=1
                        AND DepartmentId = @PAR_DepartmentId
                        AND SizePot = @PAR_SizePot
                        AND ProductSize = @PAR_ProductSize)



DELETE FROM tbl_AutoSize
                        WHERE 1=1
                        AND DepartmentId = @PAR_DepartmentId
                        AND SizePot = @PAR_SizePot
                        AND ProductSize = @PAR_ProductSize
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,731 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tomáš Zíka 256 Reputation points
    2021-09-30T14:54:54.267+00:00

    What I would do is delete with a join to the parent table and output the deleted parentIds into a table variable. Then you can delete from the parent table using those Ids.

    One time only I'm providing the DDL and data sample, but it should be you the next time. Put in some work if you expect people to help.

    create table dbo.tbl_AutoSize
    (
        Id int identity(1,1) primary key
        , DepartmentId int not null
        , SizePot int not null
        , ProductSize int not null
    )
    
    create table dbo.tbl_AutoSizeDetail
    (
        Id int identity (1,1) primary key
        , AutoSizeId int not null references tbl_AutoSize(id)
        , Filler char(10) not null
    )
    
    
    insert into dbo.tbl_Autosize with (tablockx)
    (DepartmentId, SizePot, ProductSize)
    values
    ( 1, 1, 1)
    , ( 2, 2, 2)
    
    
    insert into dbo.tbl_AutoSizeDetail with (tablockx)
    (
        AutoSizeId, Filler
    )
    values
    (1, '1a')
    , (1, '1b')
    , (2, '2a')
    
    go
    /* before */
    select 
        * 
    from dbo.tbl_AutoSizeDetail as tasd
        join dbo.tbl_AutoSize as tas
            on tas.Id = tasd.AutoSizeId
    
    declare 
        @PAR_DepartmentId int = 1
        , @PAR_SizePot int = 1
        , @PAR_ProductSize int = 1
    
    declare @IdsForDeletion table (Id int)
    
    begin transaction
        delete tasd
        output deleted.AutoSizeId into @IdsForDeletion
        from dbo.tbl_AutoSizeDetail as tasd
        join dbo.tbl_AutoSize as tas
            on tas.Id = tasd.AutoSizeId
        where 
            tas.DepartmentId = @PAR_DepartmentId
            AND tas.SizePot = @PAR_SizePot
            AND tas.ProductSize = @PAR_ProductSize
    
        delete tas
        from dbo.tbl_AutoSize as tas
        where exists
        (
            select * from @IdsForDeletion ifd
            where ifd.Id = tas.Id
        )
    commit transaction
    
    /*after */
    select 
        * 
    from dbo.tbl_AutoSizeDetail as tasd
        join dbo.tbl_AutoSize as tas
            on tas.Id = tasd.AutoSizeId
    
    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Shabbir Daruwala 176 Reputation points
    2021-10-01T10:02:10.12+00:00

    Great thanks everyone, I accept TOMZika as best solution.

    Thanks everyone for your help.

    0 comments No comments