Aydin Hassan
Aydin Hassan

Reputation: 1475

PSQL script which performs a different select depending on a variable

Hi I am creating psql reports and at the minute I have two separate reports which do the same thing other than 1 has an extra column.

The scripts install a view in to the database which is used by some php front end to show the report nicely.

I would like to combine the two and wondered if it is possible to do the following inside the view, so it gets executed every time the report is clicked:

IF(access.accessname = 'UNBLINDED')
    SELECT s.site, s.type, s.name etc
ELSE IF(access.accessname = 'BLINDED')
    SELECT s.site, s.name etc

Or if not can anyone else think of a better way to do what I am currently doing with two different scripts?

Thanks for your help

Upvotes: 0

Views: 81

Answers (1)

Rose
Rose

Reputation: 156

One common way of handling parameterized data sets is to create user defined functions. This moves the data logic to the data layer and allows the application to simply pass in a parameter (ie: SELECT * FROM new_user_function(access.accessname))

Google can help you find detailed examples and tutorials, and this may help get you started: http://wischner.blogspot.com/2009/03/creating-stored-procedure-function.html

Good luck! ~Rose

Upvotes: 2

Related Questions