Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Deleting records from Self-Referencing Table

$
0
0

I am using this query to dodge the foreign key error when trying to delete a record from a table that has primary key and referencing foreign key in the same table. It works with smaller set of data but for a table with 400,000 records it fails with error : maximum recursion limit of 500 reached. I changed maxrecursion to 3267 and even 0. but no records were returned it infact went to infinite loop..

Please help if u have an alternative solution othr than a cascade trigger(which I am already considering)


WITH    q AS

        (
        SELECT  id, siteUrl
        FROM    TestComposite
        WHERE   id = 42
                AND siteUrl = 'site1'
        UNION ALL
        SELECT  tc.id, tc.siteUrl
        FROM    q
        JOIN    TestComposite tc
        ON      tc.parentID = q.id
                AND tc.siteUrl = q.siteUrl
        )
select * 
FROM    TestComposite

WHERE   EXISTS
        (
        SELECT  id, siteUrl
        INTERSECT
        SELECT  id, siteUrl
        FROM    q

Thanks,

J


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>