我的表格具有以下架构。
CREATE TABLE public.crewcode (
crewcodeid integer NOT NULL DEFAULT nextval('crewcode_crewcodeid_seq'::regclass),
nationality character varying(20) COLLATE pg_catalog."default",
code character varying(10) COLLATE pg_catalog."default",
codenumber bigint,
isediting boolean,
CONSTRAINT crewcode_pkey PRIMARY KEY (crewcodeid))
插入一些数据
INSERT INTO public.crewcode(
crewcodeid, nationality, code, codenumber, isediting)
VALUES ('IN', '000001', 1, false);
下面有一个我从ado.net运行的过程,并得到一些结果。
当我运行此过程时,将以desc方式明智地获得单记录国籍。然后从中获取代码编号并将其递增1,然后以递增的编号添加一行。您可以在下面的存储过程中查看代码。
DECLARE
LastCodeNumber integer;
codeId integer;
editCheck boolean;
NewCodeNumber integer;
begin
SELECT crewcodeid, codenumber, isediting FROM public.crewcode where nationality=NationalityInput order by codenumber desc limit 1
INTO codeId, LastCodeNumber, editCheck;
if (codeId is NULL) then
INSERT INTO public.crewcode (nationality, code, codenumber, isediting) VALUES(NationalityInput, '000001', 1, false);
crewCode = '000001';
else
if not (editCheck) then
UPDATE public.crewcode set isediting=true where crewcodeid=codeId;
else
return;
END if;
NewCodeNumber = LastCodeNumber + 1;
crewCode = LPAD(NewCodeNumber::text, 6, '0');
INSERT INTO public.crewcode (nationality, code, codenumber, isediting) VALUES(NationalityInput, crewCode, NewCodeNumber, false);
UPDATE public.crewcode set isediting=false where crewcodeid=codeId;
END if;
end;
现在的问题是,当我并行调用存储过程时,我将创建如下SS所示的结果。
我应该怎么做才能避免出现以上SS那样的重复?我尝试添加IsEditing列,并使其在读取时为true,false,但这是行不通的,因为并行代码中的所有内容都将同时执行,因此我的解决方案无法正常工作。还有其他方法吗?
感谢帮助!