0

for instance, I want to make a view for a particular search,

create view search_in_structure as
select a,b,c,d,e
from
 t1 natural inner join t2 natural inner join t3 ...
where
 a ilike '%search_string%'
or b ilike '%search_string%'
or c ilike '%search_string%'
or f ilike '%search_string%';

it doesn't make sense because I can't modify search_string. Is there a mechanism to provide a value for search_string so it will execute the view statement with proper modification, something like :

select a,b from search_in_structure where search_string='postgresql 4ever';

if it's not possible, what solution would you recommend me to use and achieve the same result? The only solution I can think of, would be to make a function (for example, search_in_structure (IN search text, OUT a text, OUT b text ...) returns record) and call it like :

select a,b from (select search_in_structure('postgresql 4ever'));

But as I am still a postgresql noob, I want to have expert suggestions.

asked Jun 25, 2017 at 14:53

1 Answer 1

2

A function is the way to go:

create function search_in_structure(p_search_value text)
 returns table (a text, b text, c text, d text)
as
$$
select a,b,c,d,e
from t1 
 natural join t2 
 natural join t3 ...
where
 a ilike '%'||p_search_value||'%'
or b ilike '%'||p_search_value||'%'
or c ilike '%'||p_search_value||'%'
or f ilike '%'||p_search_value||'%'
$$
language sql;

Then you can do:

select *
from search_in_structure('foobar');
answered Jun 25, 2017 at 15:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.