1

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

这是错误消息:

消息 208,级别 16,状态 1,过程 spMergeDefects,第 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
    ;

谢谢你的帮助。

4

1 回答 1

0

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

之后可能还有其他问题。

以下是 T-SQL 中正确多部分命名的一些指南: Transact-SQL 语法约定

于 2012-07-16T17:15:25.003 回答