合并数据时存储过程中的语法错误

时间:2013-12-19 12:44:14

标签: sql-server sql-server-2005 stored-procedures

我正在尝试使用XML数据批量复制功能,但它在merge语句中给出了语法错误。请帮我纠正错误

alter PROCEDURE [dbo].[Matrices1D_PhysicalUnit_InsertXML]
    (@XmlData varchar(max))
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @idoc int  

    declare @tbl_Matrix1D_Dummy as table   
    (  
    MatricesPhysicalUnit1DPK  int,
    UNIT_CODE int,
    CHILDN int,
    SUCCEDN int,
    PRECEDN int,
    PROJECT int,
    WAUN int,
    MPARENT int
    )  

    EXEC sp_xml_preparedocument @idoc OUTPUT, @XmlData; --- Prepare XML Document   

    insert into @tbl_Matrix1D_Dummy 
    (MatricesPhysicalUnit1DPK  ,
    UNIT_CODE ,
    CHILDN ,
    SUCCEDN ,
    PRECEDN ,
    PROJECT ,
    WAUN ,
    MPARENT )
    (SELECT  MatricesPhysicalUnit1DPK,UNIT_CODE ,CHILDN,SUCCEDN,PRECEDN,PROJECT,WAUN,MPARENT   
    FROM OPENXML (@idoc,'//dsWSC//WBTSpecialCourse',2)  
    with (MatricesPhysicalUnit1DPK  int,
    UNIT_CODE int,
    CHILDN int,
    SUCCEDN int,
    PRECEDN int,
    PROJECT int,
    WAUN int,
    MPARENT int))

    EXEC sp_xml_removedocument @idoc   --- Remove XML Document  
    select * from @tbl_Matrix1D_Dummy;
    MERGE into tbl_Matrix1D_PhyUnitOriginal as TargetTab
    using @tbl_Matrix1D_Dummy as SourceTab
    on TargetTab.MatricesPhysicalUnit1DPK=SourceTab.MatricesPhysicalUnit1DPK
    when matched then
    update  set
    TargetTab.UNIT_CODE=SourceTab.UNIT_CODE,
    TargetTab.CHILDN=SourceTab.CHILDN,
    TargetTab.SUCCEDN=SourceTab.SUCCEDN ,
    TargetTab.PRECEDN=SourceTab.PRECEDN,
    TargetTab.PROJECT=SourceTab.PROJECT,
    TargetTab.WAUN=SourceTab.WAUN,
    TargetTab.MPARENT=SourceTab.MPARENT
    when not matched then
    insert ( MatricesPhysicalUnit1DPK ,
    UNIT_CODE ,
    CHILDN ,
    SUCCEDN ,
    PRECEDN ,
    PROJECT ,
    WAUN ,
    MPARENT) values
    (
    SourceTab.MatricesPhysicalUnit1DPK ,
    SourceTab.UNIT_CODE ,
    SourceTab.CHILDN ,
    SourceTab.SUCCEDN ,
    SourceTab.PRECEDN ,
    SourceTab.PROJECT ,
    SourceTab.WAUN ,
    SourceTab.MPARENT
    );
    END

我正在尝试使用XML数据复制大量数据,但它在MERGE上给出了语法错误

  

'MERGE'附近的语法不正确。

1 个答案:

答案 0 :(得分:0)

 using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
        {
            bulkCopy.DestinationTableName = "dbo.tbl_Matrix1D_PhyUnitOriginal";
            try
            {
                bulkCopy.WriteToServer(dsMat1DPhy.Tables[0]);
            }
            catch (Exception ex)
            {

            }
        }

实际上SQL Server 2005中没有MERGE功能。 所以这是批量复制的另一种方式,上面的存储过程也在SQL Server 2008中工作。