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

  • Home
  • SEARCH
  • 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 227341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:35:25+00:00 2026-05-11T19:35:25+00:00

I have a very simple question about transactions. (In sql server 2000, but I

  • 0

I have a very simple question about transactions. (In sql server 2000, but I guess it applies to general db. transactions).

tblPrimaryKey

PkId        
-----
1
2
3

tblForeignKey

Id   ForeignKey  
---- ----- 
1    1
2    2
3    3
4    1

I have 2 tables, one referencing the other (tblForeignKey.ForeignKey references tblPrimaryKey.PkID). Now I have some logic that alters the table of the primary key, by deleting and reinserting a key.

After deleting, the database would be of course in an inconsistent state. I looked at on old script of mine where I first dropped the relationship and recreated it afterwards. But my question is this: I learned that a transaction is atomic, so inside of a transaction inconsistent state is allowed.

So I guess something like this should work:

BEGIN TRAN eg

    DELETE tblPrimaryKey WHERE PkId = 3     
    INSERT INTO tblPrimaryKey  SELECT 3

COMMIT TRAN eg

But this doesn’t work. Can someone provide me with an example of a working transaction that applies this logic?

UPDATES :

Consistency
This characteristic means that the database should be consistent before and after the transaction.

In no case can a partial transaction be committed to the database since that would leave the database in an inconsistent state.

Doesn’t this imply that in the transaction inconsistency is possible?

UPDATE :

Some have asked me why I didn’t use an update in this case. Kind of complicated but I give it a go: the sql needed was part of a publication script that build tables from views, and then updated those tables. Since the views contained the publicationmodel, alterations of the view were made there, and only there. The rest of the script could not rely on column names to do the update.

Of course I could query for those columnnames, but it seem like a hassle at the time, so I chose not to, and instead drop constraints and rebuild them. Now I must admit I wasn’t feeling comfortable myself with that solution, so now I use indeed an update instead. I wrote a sproc to do that, if anyone nows an other solution, please let me know.

CREATE PROC usp_SyncRecords
(
 @tableName1 as nvarchar(255),
 @tableName2 as nvarchar(255), 
 @joinClause as nvarchar(255),
 @whereClause as nvarchar(1000)
)
-- this proc updates all fields in table 1 that have corresponding names 
-- in table2 to the value of the field in table2.
AS 
BEGIN 
    DECLARE @sqlClause nvarchar(4000)
    DECLARE @curFieldName nvarchar(255)
    DECLARE @sqlColumnCursorClause nvarchar(1000)
    SET @sqlClause = 'UPDATE [' + @tableName1 + '] SET '

    -- get FieldNames for second table 
    SET @sqlColumnCursorClause = 
        'DECLARE cur CURSOR FAST_FORWARD FOR SELECT name FROM syscolumns ' + 
        'WHERE id=' + CAST(object_id(@tableName2) as nvarchar(50))

    EXEC sp_executeSql @sqlColumnCursorClause


    OPEN cur
        -- compose sqlClause using fieldnames
        FETCH NEXT FROM CUR INTO @curFieldName
        WHILE @@fetch_status <> -1 
        BEGIN 
            SET @sqlClause = @sqlClause + @curFieldName  + '=' +
                                                      @tableName2 +  '.' + @curFieldName  + ','
            FETCH NEXT FROM CUR INTO @curFieldName
        END

    CLOSE cur 
    DEALLOCATE cur 

    -- drop last comma 
    SET @sqlClause = LEFT(@sqlClause,LEN(@sqlClause) -1)

    -- adding from/join/where clauses 
    SET @sqlClause = @sqlClause + ' FROM [' + @tableName1 + '] INNER JOIN [' + @tableName2 + '] '
               + 'ON ' + @joinClause +  ' WHERE '  +  @whereClause

    EXEC sp_executeSQL @sqlClause

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-11T19:35:26+00:00Added an answer on May 11, 2026 at 7:35 pm

    The cleanest solution would be to make the foreign key constraint deferred. This will postpone the checking of the constraint until COMMIT time, allowing it to be violated temporarily during the transaction. Unfortunately, this feature is apparently not available in SQL Server. On systems that do support deferred constraints, something like the following would work:

    alter table tblForeignKey
      modify constraint YourFKNameHere
        deferrable
        initially deferred;
    

    Some systems do not allow you to change a constraint’s deferrability, in which case you would have to re-create the constraint (and possibly the table).

    The SET CONSTRAINT[S] statement can be used to toggle a constraint’s deferredness, e.g. at the beginning of the transaction:

    set constraint YourFKNameHere deferred;
    

    In my experience, the ACID properties, while clearly distinct, tend to work together. For example, in your problem, you are trying to do an update which is temporarily invalid. Other users will not see any of your changes (Isolation, Atomicity) until you commit them (Durability), and no part of your transaction will have any effect (Atomicity) unless your transaction ends with the database in a consistent state (Consistency).

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

Sidebar

Ask A Question

Stats

  • Questions 195k
  • Answers 195k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I too get that error when I do :DoMatchParen, but… May 12, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer Use the TabItem.HeaderTemplate property for your tab header, and the… May 12, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer Edit controls do not support custom cue banner colors. You… May 12, 2026 at 6:57 pm

Related Questions

Our server application receives information about rows to add to the database at a
I have what looked to me at first glance a very simple problem. I
Here's my scenario: I have a simple stored procedure that removes a specific set
I'm familiar with the LAMP stack and over the years have successfully deployed a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.