I am running PostgreSQL 9.3.5 and am seeing a difference between creating a function in PSQL vs. the pgAdmin SQL window. I have a function that I want to create (and deploy) via PSQL.
The code is:
CREATE OR REPLACE FUNCTION sync_division_owner_customer_owner_number()
RETURNS trigger AS
$BODY$
DECLARE
row_count integer := 0;
cust_uid customer.customer_uid%TYPE := 0;
cust_date_last_paid customer_owner_number.date_last_paid%TYPE;
BEGIN
SELECT COALESCE(customer_uid,0) INTO cust_uid FROM division_order D WHERE D.division_order_uid = NEW.division_order_uid;
IF cust_uid = 0 THEN
RAISE EXCEPTION 'customer_uid found for function sync_division_owner_customer_owner_number';
END IF;
SELECT count(*) INTO row_count FROM customer_owner_number A WHERE A.customer_uid = cust_uid AND A.owner_number = NEW.owner_number;
CASE row_count
WHEN 0 THEN
INSERT INTO customer_owner_number(owner_number, date_first_paid, date_last_paid, customer_uid, deadfiled)
VALUES(NEW.owner_number, NEW.date_last_paid, NEW.date_last_paid, cust_uid, TRUE);
WHEN 1 THEN
SELECT A.date_last_paid INTO cust_date_last_paid FROM customer_owner_number A WHERE A.customer_uid = cust_uid AND A.owner_number = NEW.owner_number;
IF cust_date_last_paid < NEW.date_last_paid THEN
UPDATE customer_owner_number A SET A.date_last_paid = cust_date_last_paid
WHERE A.customer_uid = cust_uid AND A.owner_number = NEW.owner_number;
END IF;
END CASE;
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
When I run this file using: psql -U (user) -d test -f "C:\test\function.sql"
I get an error:
ERROR: invalid type name "customer.customer_uid%TYPE"
Yet, if I run this same exact code in the SQL window, I have no problems at all and it creates the function like it should. I need to use the %TYPE
variable, so it is not an option to change it.
I would appreciate anyone telling me what else I could to do to fix this for PSQL.