SQL Server存储过程合并命令提供无效的对象名称消息

时间:2012-07-16 17:02:24

标签: sql-server-2008 stored-procedures merge

我正在编写一个存储过程,它将来自两个数据库的两个表与相同的模式合并。我搜索了一个答案,并认为这可能是一个权限问题,但我无法解决问题。

以下是错误消息:

  

Msg 208,Level 16,State 1,Procedure spMergeDefects,Line 66
  无效的对象名称

以下是存储过程代码:

DECLARE @MergeOutput TABLE
(
ActionType  nvarchar(10),
del_guidDefectID    uniqueidentifier,
del_idintDefectID   int,
del_fkintStructureID    int,
del_fkintDefectTypeID   int,
del_tinSeverityCode tinyint,
del_intCreatedBy    int,
del_dtmDateInspected    datetime,
del_bitRepaired bit,
del_dtmRepaired datetime,
del_intRepairedBy   int,
del_tintDefectStatus    tinyint,
del_strRepairWorkOrder  varchar(20),
del_bitTempRepaired bit,
del_dtmTempRepaired datetime,
del_intModifiedBy   int,
del_dtmModified datetime,   
del_bitCircuitSpecific  bit,
del_strCircuit  varchar(20),
del_strUser1    varchar(50),
del_strUser2    varchar(50),
del_intNADefectLodID    int,
del_fkstrInspectionCycleID  varchar(10),
del_strNote varchar(255),
del_SpanDirection   varchar(10),
del_Distance    int,
del_Deleted bit,
ins_guidDefectID    uniqueidentifier,
ins_idintDefectID   int,
ins_fkintStructureID    int,
ins_fkintDefectTypeID   int,
ins_tinSeverityCode tinyint,
ins_intCreatedBy    int,
ins_dtmDateInspected    datetime,
ins_bitRepaired bit,
ins_dtmRepaired datetime,
ins_intRepairedBy   int,
ins_tintDefectStatus    tinyint,
ins_strRepairWorkOrder  varchar(20),
ins_bitTempRepaired bit,
ins_dtmTempRepaired datetime,
ins_intModifiedBy   int,
ins_dtmModified datetime,   
ins_bitCircuitSpecific  bit,
ins_strCircuit  varchar(20),
ins_strUser1    varchar(50),
ins_strUser2    varchar(50),
ins_intNADefectLodID    int,
ins_fkstrInspectionCycleID  varchar(10),
ins_strNote varchar(255),
ins_SpanDirection   varchar(10),
ins_Distance    int,
ins_Deleted bit

);

MERGE [tblMM-Defects] AS tgt
USING dbo.NGFIElecMobile.[tblMM-Defects] AS src
ON tgt.guidDefectID = src.guidDefectID
WHEN MATCHED THEN
    UPDATE SET 
    tgt.fkintStructureID = src.fkintStructureID,
    tgt.fkintDefectTypeID = src.fkintDefectTypeID,
    tgt.tinSeverityCode = src.tinSeverityCode,
    tgt.intCreatedBy = src.intCreatedBy,
    tgt.dtmDateInspected = src.dtmDateInspected,
    tgt.bitRepaired = src.bitRepaired,
    tgt.dtmRepaired = src.dtmRepaired,
    tgt.intRepairedBy = src.intRepairedBy,
    tgt.tintDefectStatus = src.tintDefectStatus,
    tgt.strRepairWorkOrder = src.strRepairWorkOrder,
    tgt.bitTempRepaired = src.bitTempRepaired,
    tgt.intModifiedBy = src.intModifiedBy,
    tgt.dtmModified = src.dtmModified,
    tgt.bitCircuitSpecific = src.bitCircuitSpecific,
    tgt.strCircuit = src.strCircuit,
    tgt.strUser1 = src.strUser1,
    tgt.strUser2 = src.strUser2,
    tgt.fkstrInspectionCycleID = src.fkstrInspectionCycleID,
    tgt.strNote = src.strNote,
    tgt.SpanDirection = src.SpanDirection,
    tgt.Distance = src.Distance

WHEN NOT MATCHED BY TARGET THEN
    INSERT 
    (fkintStructureID,
    fkintDefectTypeID,
    tinSeverityCode,
    intCreatedBy,
    dtmDateInspected,
    bitRepaired,
    dtmRepaired,
    intRepairedBy,
    tintDefectStatus,
    strRepairWorkOrder,
    bitTempRepaired,
    intModifiedBy,
    dtmModified,
    bitCircuitSpecific,
    strCircuit,
    strUser1,
    strUser2,
    fkstrInspectionCycleID,
    strNote,
    SpanDirection,
    Distance)

    VALUES
    (src.fkintStructureID,
    src.fkintDefectTypeID,
    src.tinSeverityCode,
    src.intCreatedBy,
    src.dtmDateInspected,
    src.bitRepaired,
    src.dtmRepaired,
    src.intRepairedBy,
    src.tintDefectStatus,
    src.strRepairWorkOrder,
    src.bitTempRepaired,
    src.intModifiedBy,
    src.dtmModified,
    src.bitCircuitSpecific,
    src.strCircuit,
    src.strUser1,
    src.strUser2,
    src.fkstrInspectionCycleID,
    src.strNote,
    src.SpanDirection,
    src.Distance
    )

    WHEN NOT MATCHED BY SOURCE 
        AND tgt.CheckedOutForInsp = 1 AND CheckOutID = @CheckOutID THEN
    DELETE

    OUTPUT
    $action,
    DELETED.guidDefectID,       
    DELETED.idDefectID, 
    DELETED.fkStructureID,
    DELETED.fkDefectTypeID,     
    DELETED.tinSeverityCode,        
    DELETED.CreatedBy,
    DELETED.dtmDateInspected,
    DELETED.bitRepaired,
    DELETED.dtmRepaired,    
    DELETED.RepairedBy, 
    DELETED.tDefectStatus,
    DELETED.strRepairWorkOrder,
    DELETED.bitTempRepaired,    
    DELETED.dtmTempRepaired,    
    DELETED.ModifiedBy,
    DELETED.dtmModified,        
    DELETED.bitCircuitSpecific,
    DELETED.strCircuit,
    DELETED.strUser1,
    DELETED.strUser2,
    DELETED.NADefectLodID,
    DELETED.fkstrInspectionCycleID,
    DELETED.strNote,    
    DELETED.SpanDirection,
    DELETED.Distance,
    DELETED.[Deleted],
    INSERTED.guidDefectID,      
    INSERTED.idDefectID,    
    INSERTED.fkStructureID,
    INSERTED.fkDefectTypeID,        
    INSERTED.tinSeverityCode,       
    INSERTED.CreatedBy,
    INSERTED.dtmDateInspected,
    INSERTED.bitRepaired,
    INSERTED.dtmRepaired,   
    INSERTED.RepairedBy,    
    INSERTED.tDefectStatus,
    INSERTED.strRepairWorkOrder,
    INSERTED.bitTempRepaired,   
    INSERTED.dtmTempRepaired,   
    INSERTED.ModifiedBy,
    INSERTED.dtmModified,       
    INSERTED.bitCircuitSpecific,
    INSERTED.strCircuit,
    INSERTED.strUser1,
    INSERTED.strUser2,
    INSERTED.NADefectLodID,
    INSERTED.fkstrInspectionCycleID,
    INSERTED.strNote,   
    INSERTED.SpanDirection,
    INSERTED.Distance,
    INSERTED.[Deleted]
    INTO @MergeOutput
    ;

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我猜你的USING dbo.NGFIElecMobile.[tblMM-Defects] AS src行应该是USING NGFIElecMobile.dbo.[tblMM-Defects] AS src

之后可能还有其他问题。

以下是T-SQL中正确的多部分命名的一些指导: Transact-SQL Syntax Conventions