When we try to create a view within a funcion we get ERROR: there is no parameter $1. This is the sample code.
Begin
CREATE VIEW artikelnr AS
SELECT datum, 'uitgifte' as "type", CASE WHEN 'test'='test' THEN 0 END as "aantal ontvangen", aantal as "aantal uitgegeven"
FROM uitgifteregel
JOIN artikel ON artikel.artikelnr = new.artikelnr
JOIN uitgifte ON uitgifte.uitgiftenr = uitgifteregel.uitgiftenr
UNION
SELECT datum, 'ontvangst' as "type", aantal as "aantal ontvangen" , CASE WHEN 'test'='test' THEN 0 END as "aantal uitgegeven"
FROM ontvangstregel
JOIN artikel ON artikel.artikelnr = new.artikelnr
JOIN ontvangst ON ontvangst.ontvangstnr = ontvangstregel.ontvangstnr;
Return new;
end;
When we replace new.artikelnr on line 7 with value 1 it works like it should, but the function needs to work with different artikelnr's.
example line 7: JOIN artikel ON artikel.artikelnr = new.artikelnr
Please point us in the right direction.
Response: We have to create this view for educational purposes. I have uploaded an image of the view and the tablestructure of our database:
http://img208.imageshack.us/img208/5655/tablesk.jpg
Our first goal was to create a view for one artikel. We achieved this with the following code:
CREATE VIEW artikelmutatiestotaal AS
SELECT null as "datum",'totaal' as "type",sum(ontvangstregel.aantal)as "aantal ontvangen",sum(uitgifteregel.aantal) as "aantal uitgegeven"
FROM uitgifteregel, ontvangstregel
UNION
SELECT datum,'uitgifte' as "type", CASE WHEN 'test'='test' THEN 0 END as "aantal ontvangen", aantal as "aantal uitgegeven"
FROM uitgifteregel
JOIN artikel ON artikel.artikelnr = 1
JOIN uitgifte ON uitgifte.uitgiftenr = uitgifteregel.uitgiftenr
UNION
SELECT datum,'ontvangst' as "type", aantal as "aantal ontvangen" , CASE WHEN 'test'='test' THEN 0 END as "aantal uitgegeven"
FROM ontvangstregel
JOIN artikel ON artikel.artikelnr = 1
JOIN ontvangst ON ontvangst.ontvangstnr = ontvangstregel.ontvangstnr
Only thing we can't achieve is to get the value of artikelnr out of our insert statement.
CREATE FUNCTION addview() returns trigger as '
Begin
CREATE VIEW artikelnr AS
SELECT null as "datum",'totaal' as "type",sum(ontvangstregel.aantal)as "aantal ontvangen",sum(uitgifteregel.aantal) as "aantal uitgegeven"
FROM uitgifteregel, ontvangstregel
UNION
SELECT datum,'uitgifte' as "type", CASE WHEN 'test'='test' THEN 0 END as "aantal ontvangen", aantal as "aantal uitgegeven"
FROM uitgifteregel
JOIN artikel ON artikel.artikelnr = new.artikelnr
JOIN uitgifte ON uitgifte.uitgiftenr = uitgifteregel.uitgiftenr
UNION
SELECT datum,'ontvangst' as "type", aantal as "aantal ontvangen" , CASE WHEN 'test'='test' THEN 0 END as "aantal uitgegeven"
FROM ontvangstregel
JOIN artikel ON artikel.artikelnr = artikelnr
JOIN ontvangst ON ontvangst.ontvangstnr = ontvangstregel.ontvangstnr
end;
'language plpgsql;
When we replace JOIN artikel ON artikel.artikelnr = new.artikelnr
on line 7 with
JOIN artikel ON artikel.artikelnr = 1
it works fine. Sorry for posting my question very unstructured. I don't know very good which information is important for answering this question.