我有一张像
这样的表格CREATE TABLE FinishedMappings (
partner_id UNIQUEIDENTIFIER,
survey_id INT,
PRIMARY KEY (partner_id,survey_id),
FOREIGN KEY (partner_id) REFERENCES Partners(id),
FOREIGN KEY (survey_id) REFERENCES Surveys(id)
);
这是跟踪调查的人。如果X
人未进行调查Y
,那么partner_id=X,survey_id=Y
组合就不在该表中。我创建了一个存储过程,该存储过程根据一个人是否进行了特定调查的新信息进行更新。我是如何开始写作的
CREATE PROCEDURE UpdateFinishValue (@partner_id UNIQUEIDENTIFIER,
@survey_id INT,
@finished TINYINT)
AS
IF [finished is equal to 1 and the @partner_id,@survey_id pair isnt in the FinishedMappings db]
INSERT INTO FinishedMappings (partner_id,survey_id) VALUES (survey_id,partner_id)
ELSE
DELETE FROM FinishedMappings WHERE partner_id=@partner_id AND survey_id=@survey_id
GO
但我不确定如何实施
[finished is equal to 1 and the @partner_id,@survey_id pair isnt in the FinishedMappings db]
部分或者如果有更紧凑的整体写作方式。有什么帮助吗?
答案 0 :(得分:1)
这样的事情:
if (@finished = 1 and
not exists (select 1
from FinishedMappings fm
where fm.partner_id = @parner_id and fm.survey_id = @survey_id
)
)
begin
. . .
end;