Skip to content
Advertisement

Stored procedure to return count

I am trying to make a stored procedure for the query I have:

or

I have written this stored procedure but it returns count of all the records in column not the result of query plus I need to write stored procedure in plpgsql not in SQL.

Help me write this type of stored procedure in plpgsql which returns returns the number of accounts managed by branches located in the specified city.

Advertisement

Answer

The plpgsql version could look like this:

Call:

  • Avoid naming coalitions by making the parameter name unique or table-qualifying columns in the query. I did both.

  • Just RETURN the result for a simple query like this.

  • The function is declared to integer. Make sure the return type matches by casting the bigint to int.

  • NATURAL JOIN is short syntax, but it may not be the safest form. Later changes to underlying tables can easily break this. Better to join on column names explicitly.

  • PERFORM is only valid in plpgsql functions, not in sql functions and not useful here at all.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement