Home Forums SQL Server 2008 T-SQL (SS2K8) Order change in Parent to its child tables using FK relations RE: Order change in Parent to its child tables using FK relations

  • Naveen J V (4/20/2015)


    Hi Experts,

    I have used Aasim Abdullah's (below link) stored procedure for dynamically generate code for deletion of child tables based on parent with certain filter condition. But I am getting a output which is not proper (Query 1). I would like to have output mentioned in Query 2.

    Link:

    http://stackoverflow.com/questions/485581/generate-delete-statement-from-foreign-key-relationships-in-sql-2008

    --[Patient] is the Parent table, [Case] is child table and [ChartInstanceCase] is grand child

    --When I am deleting a grand child table, it should be linked to child table first followed by Parent

    --- query 1

    DELETE Top(100000) FROM [dbo].[ChartInstanceCase]

    FROM [dbo].[Patient] INNER JOIN [dbo].[Case] ON [Patient].[PatientID] = [Case].[PatientID]

    INNER JOIN [dbo].[ChartInstanceCase] ON [Case].[CaseID] = [ChartInstanceCase].[CaseId]

    WHERE [Patient].PracticeID = '55';

    --Query 2

    DELETE Top(100000) [dbo].[ChartInstanceCase]

    FROM [dbo].[ChartInstanceCase] INNER JOIN [dbo].[Case] ON [ChartInstanceCase].[CaseId]=[Case].[CaseID]

    INNER JOIN [dbo].[Patient] ON [Patient].[PatientID] = [Case].[PatientID]

    WHERE [Patient].PracticeID = '55';

    Please do let me know how to modify the SP 'dbo.uspCascadeDelete' to get the output as Query 2

    Please help.

    Thanks,

    Naveen

    A couple challenges here. First I don't want to go read another thread somewhere else so I can help you. Secondly you mention output but there is no output from either query you posted. They are both delete statements with no output clause. I assume you must mean that it is not doing what you want? The best way to get some help is to provide us the details so we can help you. Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/