Home > Link, Microsoft SQL Server, MSBI, Optimization, Query, Script, SQL Mentalist, SQL PraRup, SQL Query, SQL Server, Technology,, TSQL, Vishal Pawar > BI SQL # 147 : SQL Server DBA Scripts : Delete rows from relational tables with foreign keys

BI SQL # 147 : SQL Server DBA Scripts : Delete rows from relational tables with foreign keys

Hi Friends,

In this article we are going to cover How to Delete rows from relational tables with foreign keys.

In this post we are going to discuss following points:

  • Problem Statement of SQL Script:
  • Description of SQL Script:
  • SQL Script Code
  • SQL Script Output Screenshot
  • User Level to execute

Problem Statement of SQL Script:

Delete rows from relational tables with foreign keys.

Description of SQL Script:

This T-SQL Script will give the tree structure, so the output of this procedure you can use to delete or insert or update the keys.

SQL Script Code

ALTER PROCEDURE pr_schema
AS
SET NOCOUNT ON

-- 1  
SELECT SCHEMA_NAME(SOF.SCHEMA_ID) AS FSC_NAME
    ,T1.FKEYID AS FK
    ,SCHEMA_NAME(SOR.SCHEMA_ID) AS RSC_NAME
    ,T1.RKEYID AS PK
INTO #sysref
FROM sysforeignkeys T1
LEFT OUTER JOIN sys.objects SOF ON SOF.OBJECT_ID = T1.FKEYID
LEFT OUTER JOIN sys.objects SOR ON SOR.OBJECT_ID = T1.RKEYID
-- below 3 lines is to avoid the problems with self reference  
LEFT OUTER JOIN SYSREFERENCES T2 ON OBJECT_NAME(T1.FKEYID) + 
    OBJECT_NAME(T1.RKEYID) = OBJECT_NAME(T2.RKEYID) + 
    OBJECT_NAME(T2.FKEYID)
WHERE T2.FKEYID IS NULL

--*******************************************************  
-- Get the Max No.Of Joins foreign keys in a Chain  
DECLARE @N VARCHAR(9);

WITH CTE (
    FKEYID
    ,LEVEL
    )
AS (
    SELECT FKEYID
        ,0
    FROM sysforeignkeys
    
    UNION ALL
    
    SELECT T1.PK
        ,LEVEL + 1
    FROM #sysref T1
    JOIN CTE T2 ON T1.FK = T2.FKEYID
    )
-- Statement that executes the CTE 
SELECT @N = Max(LEVEL)
FROM CTE

PRINT @N

--*******************************************************  
DECLARE @V1 VARCHAR(max)

SET @V1 = ''

DECLARE @V2 VARCHAR(max)

SET @V2 = ''

DECLARE @select VARCHAR(max)

SET @select = ''

DECLARE @OrderBy VARCHAR(2)

SET @OrderBy = @N + 1

WHILE NOT @N = 0
BEGIN
    SET @V1 = @V1 + ' right outer join #sysref A' + cast(@N AS 
            VARCHAR(4)) + ' on a' + @V2 + '.pk =A' + cast(@N AS 
            VARCHAR(4)) + '.fk'
    SET @select = @select + 'a' + @N + '.FSC_NAME+''.''+object_name(' 
        + 'a' + @N + '.fk) AS key' + convert(VARCHAR, (@N + 1
                )) + ','
    SET @V2 = @N
    SET @N = @N - 1

    CONTINUE
END

SET @select = @select + 
    'A1.RSC_NAME+''.''+object_name(A1.PK) AS  Key1'

EXEC (
        'SELECT ' + @select + '  from #sysref a ' + @V1 + 
        ' order by ' + @OrderBy
        )

SQL Script Output Screenshot

image

User Level to execute

    400

    Hope you will like How to Delete rows from relational tables with foreign keys.

    If you really like reading my blog and understood at least few thing then please don’t forget to subscribe my blog.

If you want daily link and analysis or interesting link go to following website which will give @ your inbox please subscribe our following link resource blog :

Link Resource Website

For More information related to BI World visit my Mentalist Blog

SQL Server Mentalist >> SQL Learning Blog

Business Intelligence Mentalist >> BI World

Infographic Mentalist >> Image worth explaining thousand Words

Microsoft Mentalist >> MVC,ASP.NET, WCF & LinQ

DBA Mentalist >>Advance SQL Server Blog

Microsoft BI Mentalist >> MS BI Development Update

Connect With me on

| FaceBook |Twitter | linkedIn| Google+ | WordPress | RSS |

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: