我在 plpgsql 中编写了一个触发器函数,它作用AFTER INSERT
于一个表。
触发器函数调用另外两个函数,第一个函数没有返回正确的值。我对此束手无策,搜索没有找到任何答案。
凸轮有人阐明了这个问题,好吗?
下面的触发函数:
CREATE TRIGGER timetotaketrigger
AFTER INSERT
ON "Prescription Schema"."TimeToTake"
FOR EACH ROW
EXECUTE PROCEDURE failtimetotakeinsert();
上述触发器按预期工作。
正在调用 failtimetotakeinsert,源为:
CREATE OR REPLACE FUNCTION failtimetotakeinsert()
RETURNS trigger AS '
DECLARE
-- declare variables to hold the information from
-- the row being inserted
-- and save the data from the new row
drug_name character(32);
drug_strength character(8);
drug_strength_unit character(16);
drug_dosage integer;
row_counts integer = 0;
frequency integer = 0;
difference integer = 0;
BEGIN
RAISE NOTICE ''Frequency at the start is %'',frequency;
-- save the data from the new row
drug_name = NEW."DrugName";
drug_strength = NEW."DrugStrength";
drug_strength_unit = NEW."DrugStrengthUnit";
drug_dosage = NEW."DrugDosage";
-- get the frequency
SELECT INTO frequency GetPrescriptionFrequency(drug_name,
drug_strength,
drug_strength_unit,
drug_dosage);
RAISE NOTICE ''frequency is %'',frequency;
-- count the rows from the current table
SELECT INTO row_counts CountTimeToTake(drug_name,drug_strength,
drug_strength_unit,drug_dosage);
RAISE NOTICE ''row counts are %'',row_counts;
-- work out the difference
difference = row_counts - frequency;
RAISE NOTICE ''Difference is %'',difference;
-- now check the two figures
IF difference > 0 THEN
RAISE EXCEPTION ''More rows than frequency requires'';
END IF;
RETURN NULL;
END;
' LANGUAGE 'plpgsql'
我遇到的问题是在这个函数中调用的第一个函数,因为当从 PgAdmin sql environemtn 调用时它没有返回值,结果如我所料。
CREATE OR REPLACE FUNCTION GetPrescriptionFrequency
(character, character, character, integer)
RETURNS integer AS '
#variable_conflict error
DECLARE
-- Declare drug_name,
-- drug_strength,
-- drug_strength_unit and
-- drug_dosage as an alias for the argument variables
-- normally referenced with the $1,$2,$3 and $4 identifiers
drug_name ALIAS FOR $1;
drug_strength ALIAS FOR $2;
drug_strength_unit ALIAS FOR $3;
drug_dosage ALIAS FOR $4;
-- declare a variable to hold the count
freq integer := 0;
BEGIN
SELECT INTO freq COUNT(*)
FROM "Prescription Schema"."PrescriptionItem"
WHERE "PrescriptionItem"."DrugName" = drug_name AND
"PrescriptionItem"."DrugStrength" = drug_strength AND
"PrescriptionItem"."DrugStrengthUnit" = drug_strength_unit AND
"PrescriptionItem"."DrugDosage" = drug_dosage;
-- IF NOT FOUND THEN
-- RAISE EXCEPTION ''prescription item not found %'', drug_strength;
-- END IF;
IF freq IS NULL THEN
RETURN 0;
ELSE
RETURN freq;
END IF;
END;
' LANGUAGE 'plpgsql'
另一个函数运行正常,但我也包含了源代码:
CREATE OR REPLACE FUNCTION CountTimeToTake(character,character,character,integer)
RETURNS integer AS '
DECLARE
-- Declare drug_name,
-- drug_strength,
-- drug_strength_unit and
-- drug_dosage as an alias for the argument variables
-- normally referenced with the $1,$2,$3 and $4 identifiers
drug_name ALIAS FOR $1;
drug_strength ALIAS FOR $2;
drug_strength_unit ALIAS FOR $3;
drug_dosage ALIAS FOR $4;
-- declare a variable to hold the count
row_count integer := 0;
BEGIN
-- count the number of TimeToTake rows for the given
-- parameter values
SELECT INTO row_count COUNT(*) FROM "Prescription Schema"."TimeToTake"
WHERE "TimeToTake"."DrugName" = drug_name AND
"TimeToTake"."DrugStrength" = drug_strength AND
"TimeToTake"."DrugStrengthUnit" = drug_strength_unit AND
"TimeToTake"."DrugDosage" = drug_dosage;
return row_count;
END;
' LANGUAGE 'plpgsql'
我已经尝试了我在互联网上可以找到的所有内容,并且在你们之前的问题中也无济于事。任何帮助将不胜感激。