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 4607790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:45:09+00:00 2026-05-22T00:45:09+00:00

I have a stored Procedure called Active and the code is: CREATE PROCEDURE dbo.Active

  • 0

I have a stored Procedure called Active and the code is:

CREATE PROCEDURE dbo.Active
        (
          @ID INT ,
          @Source VARCHAR(25)
        )
    AS 
        BEGIN
            DECLARE @SQL NVARCHAR(MAX)
            DECLARE @SchemaName SYSNAME
            DECLARE @TableName SYSNAME
            DECLARE @DatabaseName SYSNAME
            DECLARE @BR CHAR(2)
            SET @BR = CHAR(13) + CHAR(10) 


        SELECT  @SchemaName = Source_Schema ,
                @TableName = Source_Table ,
                @DatabaseName = Source_Database
        FROM    Source
        WHERE   ID = @ID


        SET @SQL = 'UPDATE Source_Table' + @BR
            + 'SET __ACTIVE = CASE WHEN rn = 1 THEN 1 ELSE 0 END' + @BR
            + 'FROM   ( ' + @BR + 'SELECT ROW_NUMBER() OVER (PARTITION BY '
            + @Source + ' ORDER BY __REC_ID DESC) AS rn
        ,  * FROM ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName
            + @BR + ') Source_Table' + @BR          

        EXEC @SQL

    END 

The problem is I am using thsi procedure in another procedure so Everytime that Procedure runs this procedure also runs and does the update to whole table.

The main reason for that update is to check for the duplicates on the table and set the duplicates to 0 and remaining to 1.

I dont want to run this update for whole table but I want the update to run only for Active duplicates.

Is there a way to do it?

  • 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-22T00:45:10+00:00Added an answer on May 22, 2026 at 12:45 am

    To reiterate your issue. You are calling the above stored procedure from another stored procedure. I assume that the parent procedure is what is determining what you call “Active duplicates”. If that is the case, then you have a few options:

    1) Temp table, have the first procedure create a global temporary table and use it in nested procedure. Make sure to clean up after.

    --Base procedure creates global temp table with proper values;
    SELECT ID 
    INTO ##ActiveDups
    FROM DUPTABLE
    WHERE SOMECONDITION = SOMECONDITION
    
    --Join global temp table on query
    SET @SQL = 'UPDATE Source_Table' + @BR
        + 'SET __ACTIVE = CASE WHEN rn = 1 THEN 1 ELSE 0 END' + @BR
        + 'FROM   ( ' + @BR + 'SELECT ROW_NUMBER() OVER (PARTITION BY '
        + @Source + ' ORDER BY __REC_ID DESC) AS rn
        ,  * FROM ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName
        + @BR + ') Source_Table' + @BR 
        + ' INNER JOIN ##ActiveDups ad ON ad.ID = Source_Table.ID'
    
    --Drop global temp table
    DROP TABLE ##ActiveDups
    

    2) Parameter, pass a comma separated list to the nested procedure and filter with IN or EXISTS clause. Not very scalable. (See added parameter and last line of query)

    CREATE PROCEDURE dbo.Active
    (
        @ID INT ,
        @Source VARCHAR(25),
        @List VARCHAR(MAX)
    )
    
    --...
    
    SET @SQL = 'UPDATE Source_Table' + @BR
        + 'SET __ACTIVE = CASE WHEN rn = 1 THEN 1 ELSE 0 END' + @BR
        + 'FROM   ( ' + @BR + 'SELECT ROW_NUMBER() OVER (PARTITION BY '
        + @Source + ' ORDER BY __REC_ID DESC) AS rn
        ,  * FROM ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName
        + @BR + ') Source_Table' + @BR 
        + ' WHERE SOMECONDITION IN ' @List
    

    3) Add logic to your dynamic SQL to fetch the proper results. (See last line, which was appended. I cannot determine for you what that logic may be.)

    SET @SQL = 'UPDATE Source_Table' + @BR
        + 'SET __ACTIVE = CASE WHEN rn = 1 THEN 1 ELSE 0 END' + @BR
        + 'FROM   ( ' + @BR + 'SELECT ROW_NUMBER() OVER (PARTITION BY '
        + @Source + ' ORDER BY __REC_ID DESC) AS rn
        ,  * FROM ' + @DatabaseName + '.' + @SchemaName + '.' + @TableName
        + @BR + ') Source_Table' + @BR 
        + ' WHERE SOMECONDITION = SOMECONDITION'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE
I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have a deprecated stored procedure which should no longer be called from code,
I have a Stored Procedure called spGetOrders which accepts a few parameters: @startdate and
I am writing a stored procedure where I have an input parameter called my_size
I have stored procedures in SQL Server T-SQL that are called from .NET within
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
So I have a stored procedure called Spr_EventLogCreate defined in my database. I have
I have a stored procedure called sp_BulkInsert that inserts one .csv file into my
I have following stored procedure code when i execute it gives an error saying

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.