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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:59:44+00:00 2026-05-24T23:59:44+00:00

I’m trying run this procedure with generic parameters. If I can’t delete because some

  • 0

I’m trying run this procedure with generic parameters.

If I can’t delete because some foreign key, it should update the row.

But when I execute it, still running forever and don’t complete the process, any Idea?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[stp_Batch_Registros_Desativados_Excluir]
    @table_name VARCHAR(100),   
    @id          int
AS
BEGIN 
        DECLARE @column VARCHAR(100),
                @sql    VARCHAR(300);

       SET @column = (SELECT COLUMN_NAME 
                      FROM INFORMATION_SCHEMA.COLUMNS 
                        WHERE COLUMNPROPERTY(OBJECT_ID(TABLE_NAME),COLUMN_NAME,'IsIdentity') = 1 
                        AND TABLE_NAME = @table_name);


    BEGIN TRY    
         select @sql = 'DELETE ' + @table_name + ' WHERE ' + @column + ' = ' + CONVERT(VARCHAR,@id);     
         exec(@sql);    
    END TRY

    BEGIN CATCH
        select @sql = 'UPDATE ' + @table_name + ' SET fg_excluido = 2 WHERE ' + @column + ' = ' + CONVERT(VARCHAR,@id) ;
        exec(@sql);
    END CATCH;
END
  • 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-24T23:59:45+00:00Added an answer on May 24, 2026 at 11:59 pm

    This seems to work fine for me. Do you have triggers, cascading foreign keys, etc.?

    USE tempdb;
    GO
    CREATE TABLE dbo.fooblat1
    (
        id INT IDENTITY(1,1) PRIMARY KEY,
        x VARCHAR(1),
        fg_excluido TINYINT NOT NULL DEFAULT 1
    );
    
    CREATE TABLE dbo.fooblat2
    (
        blatid INT NOT NULL FOREIGN KEY REFERENCES dbo.fooblat1(id)
    );
    
    INSERT dbo.fooblat1(x) SELECT 'a';
    INSERT dbo.fooblat2 SELECT SCOPE_IDENTITY();
    INSERT dbo.fooblat1(x) SELECT 'b';
    GO
    

    Then:

    CREATE PROCEDURE [dbo].[proc]
        @table_name VARCHAR(100),   
        @id         INT
    AS
    BEGIN 
        SET NOCOUNT ON;
    
        DECLARE @column NVARCHAR(100),
                @sql    NVARCHAR(MAX);
    
        SELECT @column = name
            FROM sys.columns
            WHERE [object_id] = OBJECT_ID(@table_name)
            AND is_identity = 1;
    
        BEGIN TRY    
            SET @sql = 'DELETE [' + @table_name 
                + '] WHERE [' + @column + '] = ' 
                + CONVERT(VARCHAR(12),@id);     
    
            EXEC sp_executesql @sql;    
        END TRY
        BEGIN CATCH
            SET @sql = 'UPDATE [' + @table_name 
                + '] SET fg_excluido = 2 WHERE ' 
                + @column + ' = ' + CONVERT(VARCHAR(12),@id);
    
            EXEC sp_executesql @sql;
        END CATCH;
    END
    GO
    

    Then:

    EXEC [dbo].[proc] 'fooblat1', 1; -- should update
    GO
    EXEC [dbo].[proc] 'fooblat1', 2; -- should delete
    GO
    SELECT * FROM dbo.fooblat1;
    GO
    

    Cleanup:

    DROP PROC [dbo].[proc];
    DROP TABLE dbo.fooblat2;
    DROP TABLE dbo.fooblat1;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.