I have searched and could not find an open post regarding this topic:
I'm working with stored procedures in PostgreSQL and I'm currently being asked to create one that will collect certain information from a publisher based on the only parameter passed, their publishercode.
This code is in the existing table as a "character" type and as such I'm passing it the same and calling the function with that same type input, but I'm only obtaining a "NULL" value from my input and I'm not sure why.
This is my code:
CREATE OR REPLACE FUNCTION uspGetPubStatsMAC (pubcode publisher.publishercode%TYPE) -- Publisher Code
RETURNS text AS
$$
DECLARE PubCode publisher.publishercode%TYPE;
PubName text;
NumOfAuth int;
NumOfPubBooks int;
HiValBook text;
NumHiValBook int;
TotNumHiValBook int;
BEGIN
-- Get the publisher's name from the given code
RAISE NOTICE 'Code: (%)', pubcode;
SELECT publishername INTO PubName
FROM publisher
WHERE publishercode = pubcode;
RAISE NOTICE 'Name: (%)', PubName;
-- check if a publisher exists with the given code
IF ( PubName is null )
THEN
RAISE NOTICE 'No publisher exists with the given code (%)', pubcode;
RETURN (-1);
END IF;
RAISE NOTICE 'Current Statistics of the publisher (%)', pubcode;
RAISE NOTICE ' The Name of the Publisher: (%)', trim (trailing ' ' from PubName);
SELECT count(distinct a.authornum) INTO NumOfAuth
FROM publisher p, book b, wrote w, author a
WHERE p.publishercode = b.publishercode
and b.bookcode = w.bookcode;
RAISE NOTICE ' The number of distinct authors who have written book(s) for this publisher: (%)', NumOfAuth;
END;
$$ language plpgsql;
SELECT uspGetPubStatsMAC('AH');
Where 'AH' is the code corresponding to publisher Arkham House in my table. Any idea of what I'm doing wrong?