Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1015249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:22:21+00:00 2026-05-16T10:22:21+00:00

I need to get stored procedure (not standart, only custom) to delete unused rows

  • 0

I need to get stored procedure (not standart, only custom) to delete unused rows table (table1) and some other tables (table2, table3, table4…- we don’t know how many tables refers to table1 by FOREIGN KEY CONSTRAINT) can refers to this table.

For example:

СREATE PROCEDURE [dbo].[delete_on_constraints]
@table varchar(36) – table name we need to delete unused rows
@column varchar(36) – column name - primary key, on which other tables refers by FOREIGN KEY CONSTRAINT

There are the table1 and table2, table3 which refers to table1 (remind, we don’t know how many tables refers to table1 by FOREIGN KEY CONSTRAINT)

CREATE TABLE [table1] (
    [id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table1] PRIMARY KEY,
    [value] [int] NULL 
)
GO

CREATE TABLE [table2] (
    [id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table2] PRIMARY KEY,
    [table1_id] [int] NOT NULL ,
       CONSTRAINT [FK_ table2_ table1] FOREIGN KEY ([table1_id]) REFERENCES [table1] ( [id] ) 
) 
GO 

CREATE TABLE [table3] (
    [id] [int] IDENTITY (1, 1) NOT NULL CONSTRAINT [PK_ table3] PRIMARY KEY,
    [table1_id] [int] NOT NULL ,
       CONSTRAINT [FK_ table3_ table1] FOREIGN KEY ([table1_id]) REFERENCES [table1] ( [id] ) 
)

GO 
INSERT INTO [table1] VALUES (100)
INSERT INTO [table1] VALUES (200)
INSERT INTO [table1] VALUES (300)
INSERT INTO [table1] VALUES (400)

GO
INSERT INTO [table2] VALUES (1)
INSERT INTO [table2] VALUES (3)

GO
INSERT INTO [table3] VALUES (1)
INSERT INTO [table3] VALUES (2)
INSERT INTO [table3] VALUES (3)

There are rows in tables before call stored procedure

table1
id value
1 100
2 200
3 300
4 400

table2
id table1_id
1 1
2 3

table3
id table1_id
1 1
2 2
3 3

And after [dbo].[delete_on_constraints] ‘table1’ ‘id’ procedure call

table1

id       value
1        100
2        200
3        300
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:22:22+00:00Added an answer on May 16, 2026 at 10:22 am

    First, let me state for the record that this code should never be used in production. By that I mean, it should only ever be used as a one-time cleanup. If you have regular need of deleting rows without knowing the DRI, then there is a fundamental design problem.

    In short, you need to use dynamic SQL to query for the relations and build the Delete statements.

    Declare @Columns Cursor
    Declare @PKTableSchema nvarchar(128)
    Declare @PKTableName nvarchar(128)
    Declare @PKColumnName nvarchar(128)
    Declare @TableSchema nvarchar(128)
    Declare @TableName nvarchar(128)
    Declare @ColumnName nvarchar(128)
    Declare @Sql nvarchar(max)
    
    Set @Columns = Cursor Fast_Forward For
        Select Quotename(PK_CCU.TABLE_SCHEMA)
            , Quotename(PK_CCU.TABLE_NAME)
            , Quotename(PK_CCU.COLUMN_NAME)
            , Quotename(RC_CCU.TABLE_SCHEMA)
            , Quotename(RC_CCU.TABLE_NAME)
            , Quotename(RC_CCU.COLUMN_NAME)
        From INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As RC
            Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As RC_CCU
                On RC_CCU.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
                    And RC_CCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
            Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
                On TC.CONSTRAINT_SCHEMA = RC.UNIQUE_CONSTRAINT_SCHEMA
                    And TC.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
                    And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
            Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As PK_CCU
                On PK_CCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
                    And PK_CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
        Where PK_CCU.TABLE_SCHEMA = 'dbo'
            And PK_CCU.TABLE_NAME = @TableParam
            And PK_CCU.COLUMN_NAME = @ColumnParam
    
    Open @Columns 
    Fetch Next From @Columns Into @PKTableSchema, @PKTableName, @PKColumnName, @TableSchema, @TableName, @ColumnName
    
    Set @Sql = 'Delete ' + @PKTableSchema + '.' + @PKTableName 
        + ' Where 1 = 1' 
    
    While @@Fetch_Status = 0
    Begin
        Set @Sql = @Sql + Char(13) + Char(10)
            + ' And Not Exists ( Select 1 From ' + @TableSchema + '.' + @TableName
                                + ' Where ' + @ColumnName + ' = ' + @PKTableSchema + '.' + @PKTableName + '.' + @PKColumnName
                                + ')'
        Fetch Next From @Columns Into @PKTableSchema, @PKTableName, @PKColumnName, @TableSchema, @TableName, @ColumnName
    End
    
    Close @Columns
    Deallocate @Columns
    
    --Print @Sql
    Exec( @Sql )
    

    Btw, let me also mention that a better solution might be to enable Cascade Delete on your relations. With cascade delete, the need for this routine would be moot.

    See Cascading Referential Integrity Constraints for more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to pass a parameter to stored procedure to get all rows which
In an SQL Server procedure, I need to get all rows matching some constraints(simple
I need to create a stored procedure one time and in one place only
I need to create a stored procedure which will get destination IP and then
I need to use stored procedure to get data from DB, but I would
I need to create a Postgres 9.1 PL/pgSQL stored procedure that, among other parameters,
I am new to PHP. I need to GET a xml stored locally using
I have created 5 NSDictionary's. I stored them in NSArray. I need to get
I need to store locally on android devices some images I get from the
Just need get some vals located in application.ini(main ini) in the Controller plugin I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.