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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:30:15+00:00 2026-05-15T22:30:15+00:00

SQL Server 2008. I have a parent row with pk id 1. While blocking

  • 0

SQL Server 2008.

I have a parent row with pk id 1. While blocking all other DB users (this is a cleanup operation so resource contention is not an issue), I would like to insert a new row, then take all of the child rows and change their fk column to the new row. With the below DDL for example, I would like to insert a new row and give all of the #chi.parid values a value of ‘3’ so they would essentially now belong to the new row so the old one can be deleted.

Help!

create table #par ( parid int identity(1,1) , note varchar(8) )
create table #chi ( chiid int identity(1,1) , parid int , thing varchar(8) )
insert into #par values ( 'note1' )
insert into #par values ( 'note2' )
insert into #chi values ( 1 , 'a' )
insert into #chi values ( 1 , 'b' )
insert into #chi values ( 1 , 'c' )
  • 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-15T22:30:15+00:00Added an answer on May 15, 2026 at 10:30 pm

    Thanks all for the input. It appears I “can’t” do a set-based operation on this with SQL Svr 2008, so I did RBAR solution with a loop (I think it performs better than a cursor). Anyone who can comment on making this safer with try..catch or enlighten me more on doing this in a set, please comment. 🙂

    Thanks.

        Select 
                    [parid]
                ,   [name]
        Into    #redo
        From    partable 
        Where   DateDiff( Hour , donewhen ,SysDateTimeOffset() ) > 23
    Begin Transaction
    Declare @rows int  = ( Select COUNT(*) From #redo )
    Declare @parid int 
    Create Clustered Index redoix on #redo([parid]) With FillFactor = 100
    While @rows > 0
    Begin
        Select Top 1 @parid = [parid] from #redo Order By parid Asc
    
            Insert partable 
                (
                    [name]
                )
            Select 
                    [name]      
            From #redo 
            Where parid = @parid
            Update chitable
                Set parid = Scope_Identity()
                Where   parid = @parid
            Delete From partable
                Where   parid = @parid
    
        Delete from #redo where [parid] = @parid 
        Set @rows  = ( Select COUNT(*) From #redo )
    End
    Commit Transaction
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple temp-table defined in SQL Server 2008 R2 representing a parent-child
In SQL Server 2008 I have two tables. One table for users: id_user -
I'm using T-Sql with SQL Server 2008. Say I have parent table: Projects: ProjectID
In SQL Server 2008, I have my parent table in one database, and the
I'm using SQL server 2008 I have the following SQL getting all dates between
for SQL Server 2008 R2 I have a resultset that looks like this (note
I'm using SQL Server 2008 and I have a view that looks like this:
I am using SQL Server 2008. I have a table like this: UnitId ParentId
Does SQL Server 2008 have a a data-type like MySQL's enum ?
In SQL Server 2008 I have a user table like the following: Userid Username

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.