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.
1 Answer 1
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');