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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:36+00:00 2026-06-11T02:55:36+00:00

I currently have a stored procedure that copies content from one table to another.

  • 0

I currently have a stored procedure that copies content from one table to another.

However when it is trying to only insert 27 new rows it continues on for over 12 minutes (after which point I stopped it) it said Affected 27 rows 4 times, however changes were not made.

Can you spot any reason the following SP would be slow?

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[sp_CopyCompanyContent]
(
        @intCopyFromCompanyID Int,
        @intNewCompanyID Int
)
As
Begin
    /*
        RaisError If any of Odl/New Company ID's are 0
    */

    If (@intCopyFromCompanyID = 0 Or @intNewCompanyID = 0)
        Begin
            RaisError('New Company ID or Old Company ID can not be 0', 16, 1)
            Return
        End


    /*
        Create Temp Table For the Content Sections
    */

    If Object_ID('tempdb..#ContentSections') IS Not Null
        Begin 
            Drop Table dbo.#ContentSections
        End 

    /*
        Have to get all the existing data for the Old company we are copying from.
        Have to also add the Max(ContentSectionID) From ContentSection. Max(ContentSectionID) + 
        The Identity (Added further down due to laziness) will be our seed for the ContentID
    */

    Select      CS.ContentID,
                CS.SectionID,
                CS.MenuOrder,
                @intNewCompanyID NewCompanyID,
                CS.CompanyID OldCompanyID,
                CS.SubMenu,
                CS.Link,
                CS.HeaderMenu,
                CS.ParentContentID,
                CRS.*

    Into        dbo.#ContentSections

    From        dbo.Company COMP
    Join        dbo.ContentSection CS
    On          COMP.Company_id = CS.CompanyID  
    Join        dbo.Content CONT
    On          CONT.ContentID = CS.ContentID

    Cross Join  (
                    Select  MAx(ContentSectionID) MaxContentSectionID
                    From    dbo.ContentSection CONT
                ) crs

    Where       COMP.Company_id  = @intCopyFromCompanyID
    Order By    COMP.Company_id


    /*
        We now need to create a table for the existing content for the old company.
        Also have to create the seed. Same principle as above.
    */


    If Object_ID('tempdb..#Content') IS Not Null
        Begin 
            Drop Table dbo.#Content
        End 

    Select  CONT.*, 
            CRS.*

    Into    dbo.#Content

    From    dbo.Company COMP
    Join    dbo.ContentSection CS
    On      COMP.Company_id = CS.CompanyID  
    Join    dbo.Content CONT
    On      CONT.ContentID = CS.ContentID

    Cross Join  (
                    Select  MAx(ContentID) MaxContentID
                    From    dbo.Content CONT
                ) crs

    Where   COMP.Company_id  = @intCopyFromCompanyID
    Order By COMP.Company_id

    /*  
        Add Identity to each of the tables we have created above. The ID fields will add to 
        the Max of each table to mimic what the future seeds will be.
    */

exec('Alter table #ContentSections Add ID Int Identity(1,1)')
exec('Alter table #Content Add ID Int Identity(1,1)')

    /*
        Add content data from the temp table.
    */


    Insert  Into dbo.Content
    (
            Title,
            Content
    )   
    Select  Title,
            Content 
    From    dbo.#Content

    /*
        Have to the Content table up to the content sections table
        as this contains what ID has been add to the Content Table. 
    */


    Insert Into dbo.ContentSection
    (
            ContentID,
            SectionID,
            MenuOrder,
            CompanyID,
            SubMenu,
            Link,
            HeaderMenu,
            ParentContentID
    )

    Select  C.MaxContentID + C.ID,
            CS.SectionID,
            CS.MenuOrder,
            CS.NewCompanyID,
            CS.Submenu,
            CS.Link,
            CS.HEaderMEnu,
            CS.ParentContentID
    From    dbo.#Content C
    Join    dbo.#ContentSections CS
    On      C.ID = CS.ID

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-06-11T02:55:37+00:00Added an answer on June 11, 2026 at 2:55 am

    First thing to do is check the query plan for the selects since cross joins are dangerous beasts, if i’m not reading it wrong you’d display the same value in CRS.* in every record right?
    If so, make that query before the select and stored the result in a variable and display it in the select, something like this.

    DECLATE @maxValue INTEGER
    Select  @maxValue=MAX(ContentID) MaxContentID
                        From    dbo.Content CONT
    Select  CONT.*, 
                @maxValue as MaxContentID
    
        Into    dbo.#Content
    
        From    dbo.Company COMP
        Join    dbo.ContentSection CS
        On      COMP.Company_id = CS.CompanyID  
        Join    dbo.Content CONT
        On      CONT.ContentID = CS.ContentID
        Where   COMP.Company_id  = @intCopyFromCompanyID
        Order By COMP.Company_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL stored procedure that will return only one record. However, in
I currently use SQL2008 where I have a stored procedure that fetches data from
I have a stored procedure that inserts into a table (where there is an
I have a stored procedure that is currently running, and seems to hang/lock on
I have a stored procedure that inserts a row into a table with an
I have a stored procedure for SQL Server 2000 that can only have a
Hello All I am using infragsitics tree view control.I have one stored procedure that
Helo, My question is I have one Stored Procedure in SQL Server that returns
I currently have the following named query that wraps around a stored procedure:- <hibernate-mapping>
I have a stored procedure that returns a valueI call it from other stored

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.