开发者社区> 问答> 正文

插入无法在SQL Server中使用MERGE工作

我在下面的查询中有一个存储的proc,用于在SQL Server中使用MERGE插入/更新,但该查询对于更新工作正常,但不适用于插入。尽管我在Target中获得了正确的更新记录,但是对于新插入,它失败了。

CREATE PROCEDURE [SA].[Insert_update_manualassessment_bak] (@QuestionData XML = 
NULL, 
                                                           @ProjectId    BIGINT 
, 
                                                           @AssesmentId  INT) 
AS 
 BEGIN 
     BEGIN try 
         SET nocount ON; 

         DECLARE @ProjectId   BIGINT, 
                 @AssesmentId INT 

         SET @ProjectId= 1000257050 
         SET @AssesmentId=0 

         DECLARE @QuestionData XML=NULL 

         SET @QuestionData = '<?xml version="1.0" ?>         <self>         <selfDetails>         <ProjectId>1000257050</ProjectId>         <QuestionId>2</QuestionId>         <AssesmentId>0</AssesmentId>         <Response>Yes</Response>         <CreatedBy>756624</CreatedBy>         <CreatedDate>12/6/2019 7:43:33 PM</CreatedDate>         <Remarks>Work Work</Remarks>         <status>InProgress</status>         <ModifiedBy>678152</ModifiedBy>         </selfDetails>         </self>' 

         DECLARE @tempQuestionDetails TABLE 
           ( 
              projectid   BIGINT, 
              questionid  BIGINT, 
              assesmentid BIGINT, 
              response    VARCHAR(4000), 
              createdby   VARCHAR(50) NULL, 
              createddate DATETIME, 
              remarks     VARCHAR(255) NULL, 
              status      VARCHAR(50), 
              modifiedby  VARCHAR(50) NULL 
           ) 

         INSERT INTO @tempQuestionDetails 
         SELECT questiondetails.query('ProjectId').value('.', 'bigint') 
                AS 
                ProjectID, 
                questiondetails.query('QuestionId').value('.', 'bigint') 
                AS 
                QuestionId, 
                questiondetails.query('AssesmentId').value('.', 'bigint') 
                AS 
                AssesmentId, 
                questiondetails.query('Response').value('.', 'varchar(50)') 
                AS 
                Response 
                , 
                questiondetails.query('CreatedBy').value('.', 'nvarchar(50)') 
                AS 
                CreatedBy, 
                questiondetails.query('CreatedDate').value('.', 'datetime') 
                AS 
                CreatedDate, 
                questiondetails.query('Remarks').value('.', 'varchar(255)') 
                AS 
                Remarks, 
                questiondetails.query('status').value('.', 'varchar(50)') 
                AS 
                Status, 
                questiondetails.query('ModifiedBy').value('.', 'varchar(50)') 
                AS 
                ModifiedBy 
         FROM   @QuestionData.nodes('/self/selfDetails') AS 
                QuestionData( 
                questiondetails) 

         SELECT * 
         FROM   @tempQuestionDetails 

         MERGE INTO sa.assessmentresponses AS TARGET 
         using (SELECT projectid, 
                       questionid, 
                       assesmentid, 
                       response, 
                       createdby, 
                       createddate, 
                       remarks, 
                       status, 
                       modifiedby 
                FROM   @tempQuestionDetails) AS source 
         ON TARGET.questionid = source.questionid 
         --AssmentResponseTarget.ProjectId= @ProjectId and AssmentResponseTarget.AssesmentId= @AssesmentId 
         WHEN matched THEN 
           UPDATE SET 
         --AssmentResponseTarget.[ProjectID] = source.[ProjectID] 
         --,AssmentResponseTarget.QuestionId = source.QuestionId 
         --,AssmentResponseTarget.AssesmentId = source.AssesmentId 
         TARGET.response = source.response, 
         TARGET.createdby = source.createdby, 
         TARGET.createddate = source.createddate, 
         TARGET.remarks = source.remarks, 
         TARGET.[status] = source.status, 
         TARGET.modifiedby = source.modifiedby 
         --source.projectId=@ProjectId and source.AssesmentId=@AssessmentId 
         WHEN NOT matched BY target THEN 
           INSERT ( projectid, 
                    questionid, 
                    assesmentid, 
                    response, 
                    createdby, 
                    createddate, 
                    remarks, 
                    [status], 
                    modifiedby ) 
           VALUES ( source.projectid, 
                    source.questionid, 
                    source.assesmentid, 
                    source.response, 
                    source.createdby, 
                    Getdate(), 
                    source.remarks, 
                    source.status, 
                    source.modifiedby ); 
     END try 

     BEGIN catch 
         DECLARE @Message VARCHAR(4000); 

         SELECT @Message = Error_message() 

         EXEC [dbo].[Errorlog] 
           '[SA].[Insert_Update_ManualAssessment]', 
           @Message 
     END catch 
 END

更新对我来说工作正常,仅在插入时出现问题。

展开
收起
心有灵_夕 2019-12-10 17:52:06 445 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载