Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 707801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:19:08+00:00 2026-05-14T04:19:08+00:00

When we try to create a view within a funcion we get ERROR: there

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:19:09+00:00Added an answer on May 14, 2026 at 4:19 am

    What database is this?

    What is this new keyword? Is this code copied from some trigger?

    • Replace new with correct table name in JOIN clause.
    • remove Return new;

    If I remember correctly, MsAccess reports similar error when you misspell field name.
    Next thing would be to check all field names. Alternatively, use some visual query builder and create those union queries to be sure that all field names are correct.

    I think that you can loose those CASE statements and write just 0 AS "Some Fieldname"

    Try this. It should work. (I did not test it against database)

    CREATE OR REPLACE VIEW artikelnr AS
      SELECT datum, 'uitgifte' as "type", 0 as "aantal ontvangen", 
           aantal as "aantal uitgegeven"
      FROM uitgifteregel 
      JOIN artikel ON artikel.artikelnr = uitgifteregel.artikelnr
      JOIN uitgifte ON uitgifte.uitgiftenr = uitgifteregel.uitgiftenr
    
    UNION
      SELECT datum, 'ontvangst' as "type", aantal as "aantal ontvangen" , 
             0 as "aantal uitgegeven"
      FROM ontvangstregel 
      JOIN artikel ON artikel.artikelnr = ontvangstregel.artikelnr
      JOIN ontvangst ON ontvangst.ontvangstnr = ontvangstregel.ontvangstnr;
    

    Edit:
    I don’t know much about Postgresql, but you probably should look into EXECUTE SCRIPT – send artikelnr as parameter and then construct DDL for view in script.

    Based on my experience with other databases (Oracle and SQL server), I don’t think that you should create one view for each record. What are you trying to accomplish with this approach?

    If goal is to have precompiled views and this gain execution speed, same thing can probably be achieved with parametrized query. Put it into stored procedure and send artikelnr as parameter.

    If it’s not for performance reason, can you please explain why are you doing it like this.

    Edit 2:
    Re answer: You cannot create view that way. Problem is that CREATE VIEW takes statements without replacing values – it tries to create view with new.artikelnr and then it fails because you don’t have table new outside of trigger. You need to construct statement as string or as file and then execute that statement. I checked documentation for PostgreSQL and there is EXECUTE command that is used for constructing and executing dynamic commands. Something like this should probably work:

    EXECUTE 
      'CREATE OR REPLACE VIEW artikelnr AS '
      || ' SELECT datum, 'uitgifte' as "type", 0 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" , '
      || '        0 as "aantal uitgegeven" '
      || ' FROM ontvangstregel  '
      || ' JOIN artikel ON artikel.artikelnr = '
      || new.artikelnr
      || ' JOIN ontvangst ON ontvangst.ontvangstnr = ontvangstregel.ontvangstnr; '
    

    This is one giant concat with new.artikelnr value inserted on right places. It “inserts” literal value of artikelnr into SQL code and then you get SQL that is executed via EXECUTE statement.

    Please note that above code does not take care about quoting and it definitely will not work as is (since this example is for educational purposes, it is left to reader to correct it as exercise :).

    In the documentation for EXECUTE statement you will find examples that use quoting functions which should be used to correctly construct above command.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 431k
  • Answers 431k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I resolved this by doing the following: Outside of the… May 15, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer You would use... $body = "From: $firstname_field $lastname_field\n Learned about… May 15, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer You're working too hard. Don't use dataStore.getFloat3DClass() when you're making… May 15, 2026 at 2:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.