0

这是我的SP:

   USE [x]
GO
/****** Object:  StoredProcedure [dbo].[sp_CopyCompanyContent]    Script Date: 09/10/2012 12:35:29 ******/
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

当我运行它时,我得到以下信息:

例如:

(41 行受影响)

(41 行受影响)

消息 207,级别 16,状态 3,过程 sp_CopyCompanyContent,第 122 行无效的列名称“ID”。消息 207,级别 16,状态 3,过程 sp_CopyCompanyContent,第 122 行无效的列名称“ID”。消息 207,级别 16,状态 3,过程 sp_CopyCompanyContent,第 122 行无效的列名称“ID”。

(1 行受影响)

谁能看到为什么它说无效的列 ID?它应该完美地加入和填充内容..

4

1 回答 1

2

尝试替换代码:

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

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

或添加到您的“选择进入”身份列,如下所示:

SELECT  ID=IDENTITY (int, 1, 1) ....
INTO    #Table
FROM    Table

例如:

 Select         ID=IDENTITY (int, 1, 1),
                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
于 2012-09-10T11:06:11.870 回答