0

Requirment is we have 10 column in table and in frontend presented as grid of 10 column and a search box with each.

Upon user enter a search in any one column, that column name and value should be passed into where clause.

sample Table

  number    agency        brand   product   ad_product  indication
  1234      salesforce    BMW      sxx       suv          E-class
  5678      apple         iphone   i-14      accessories  B-class

When user search in 'brand' as 'BMW' and ENTER values should be passed like

   select *,'Primary' "type"  from dev.sales where %brand% = %'BMW'%

and When user search in 'agency' as 'apple' and ENTER values should be passed like

   select *,'Primary' "type"  from dev.sales where %agency% = %'apple'%

1 Answer 1

1

You can do this with a function that uses dynamic SQL. Assuming that your table is called the_table then here it is.

create or replace function dynamic_search(column_name text, search_value text)
returns setof the_table language plpgsql as
$$
declare 
    DYNSQL constant text := 'SELECT * FROM the_table where %I = $1';
begin
    return query execute format(DYNSQL, column_name) using search_value;
end;
$$;

And then query

select *, 'Primary' as "type" 
from dynamic_search('brand', 'BMW');

Please note that this function is SQL injection prone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.