我在下面的查询中有一个存储的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
更新对我来说工作正常,仅在插入时出现问题。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。