テーブルに対して実行されるトリガー関数を plpgsql に記述AFTER INSERT
しました。
トリガー関数は他の 2 つの関数を呼び出しますが、最初の関数は正しい値を返しません。私はこれで頭がいっぱいで、検索しても答えが見つかりませんでした。
誰でも問題に光を当ててください。
以下のトリガー関数:
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 環境から呼び出されたときにまだ値を返していないためです。結果は期待どおりです。
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'
私はインターネット上で見つけることができるすべてのものを試しましたが、以前の質問の中でも役に立ちませんでした. どんな助けでも大歓迎です。