插入返回后无法在函数中使用 id

Cant use id in function after insert returning into

我构建了一个插入 1 个样本的函数,然后插入 4 个组,执行一些操作并调用另一个函数,最后用其余数据更新原始样本。

这是函数:

-- Fill time dimension from timestamp
CREATE OR REPLACE FUNCTION build_sample() RETURNS void AS $$
DECLARE 
--CONSTANTS
c_short CONSTANT INTERVAL := '5 minutes';
c_medium  CONSTANT INTERVAL := '20 minutes';
c_long CONSTANT INTERVAL := '120 minutes';
c_samplePeriod CONSTANT int := 86400;

size int;
startDateTime timestamp;
endDateTime timestamp;

timeDimensionId int;
weatherId int;

this_taxiid int;
this_sampleid int;

this_quickstops int;
this_moderatestops int;
this_longstops int;

stationarytime int;
movingtime int;

result int;
BEGIN
startDateTime := '2008-02-06 00:00:00';
endDateTime := startDateTime + INTERVAL '1 second' * c_samplePeriod;
this_taxiid := 366;
weatherId:=254;

-- Get timeid
Select distinct "refidHour" from "TimeDimension" into timeDimensionId 
where timestamp=startDateTime;

-- Insert basic data in a sample
INSERT INTO "Samples" ("period","refidTaxi","refidTime","refidWeather") 
VALUES(c_samplePeriod,this_taxiid,timeDimensionId,weatherId)
RETURNING sampleid into this_sampleid;

RAISE NOTICE 'sampleid=%',this_sampleid;

--Sample moving group
Insert into "EntryGroup" Values(this_sampleid,1); 
--Sample shortstops group
Insert into "EntryGroup" Values(this_sampleid,2); 
--Sample mediumstops group
Insert into "EntryGroup" Values(this_sampleid,3); 
--Sample longstops group
Insert into "EntryGroup" Values(this_sampleid,4); 

Select build_stops(this_taxiid, startDateTime, this_sampleid);

--#Quick stops
Select count(*) into this_quickstops from "EntryGroup" 
where "refidSample" =this_sampleid and "refidType"=2;
--#Moderate stops
Select count(*) into this_moderatestops from "EntryGroup" 
where "refidSample"=this_sampleid and "refidType"=3;
--#Long stops
Select count(*) into this_longstops from "EntryGroup" 
where "refidSample"=this_sampleid and "refidType"=4;

RAISE NOTICE 'quickstops=% moderatestops=% longstops=%',this_quickstops, this_moderatestops, this_longstops;

--Calculate stationary time and moving time
Select this_quickstops*c_short+this_moderatestops*c_medium+this_longstops*c_long into stationarytime;
Select c_samplePeriod-stationarytime into movingtime;

RAISE NOTICE 'statironarytime=% movingtime=%',statironarytime, movingtime;

-- Update sample with rest of data
UPDATE "Samples" SET ("stationarytime","movingtime","quickstops","moderatestops","longstops") = (stationarytime,movingtime,this_quickstops,this_moderatestops,this_longstops)
where sampleid=this_sampleid;

RAISE NOTICE 'period=% refidTaxi=% refidTime=%',c_samplePeriod,this_taxiid,timeDimensionId;

END;
$$ LANGUAGE plpgsql;

我收到以下错误

snowflake=# select build_sample();
NOTICE:  sampleid=3
ERROR:  insert or update on table "EntryGroup" violates foreign key constraint "fk_Entries_has_Samples_Samples1"
DETAIL:  Key (refidSample)=(1) is not present in table "Samples".
CONTEXT:  SQL statement "Insert into "EntryGroup" Values(this_sampleid,1)"

当该函数尝试插入一个组,该组具有之前插入的样本的 sampleid。

我正在做的事情在交易方面有问题吗,我的意思是,因为交易还没有完成,这是否意味着样本没有真正插入,我不能为那个样本插入一个组?

Is there a problem tansaction wise of doing what im doing, i mean, because the transaction hasn't finished does it mean that the sample isnt really inserted and i cant insert a group for that sample?

没有。您可能不匹配插入中的列。

始终为持久化的 INSERT 语句提供目标列表(极少数例外情况适用)。在您的函数中,替换:

<strike>--Sample moving group
Insert into "EntryGroup" Values(this_sampleid,1); 
--Sample shortstops group
Insert into "EntryGroup" Values(this_sampleid,2); 
--Sample mediumstops group
Insert into "EntryGroup" Values(this_sampleid,3); 
--Sample longstops group
Insert into "EntryGroup" Values(this_sampleid,4);</strike>

有了这个:

INSERT INTO "EntryGroup"<b>("refidSample", "refidType")</b>
SELECT this_sampleid, i.type
FROM  (VALUES (1), (2), (3), (4)) i(type);

根据其余代码猜测列名。使用 实际 目标列名称。

同时,我用一个(更便宜的)多行插入物替换了您的四个插入物。