Solution to allow users to specify which columns of a sql report to search

10-11  Source: Network gathering  Views:0 

Starting Point:
1) You have an existing SQL Report.
2) You want to provide functionality to search through the result set.
Start -----
1) add a text_item for searching PX_search where X is page number
2) add a button which resubmits the page. Go
3) You modify your query by adding the following to the end of the where clause.
and (PX_SEARCH is null or instr(column_name1,PX_SEARCH) > 0 )
4) So far so good but it only allows searches on 1 column, so you add a few other columns
and (PX_SEARCH is null or instr(column_name1,PX_SEARCH) > 0 or instr(column_name2,PX_SEARCH) > 0 or instr(column_name3,PX_SEARCH) > 0 )
Now when you enter some text if it is in any of the columns that row will be displayed.
This can be good or bad depending on your requirement. lets say my users want to specify the column they are searching on.
5)further enhancement
i) add a LOV to your page which queries the all_users data dictionary view
select column_name d, column_name r from all_users where table_name = 'MY_TABLE_NAME' and column_name not in ('COLUMN_NAME1,COLUMN_NAME2) order by column_name
The idea here is to use the oracle data dictionary to get all the columns for your table(s) and ignore the columns you do not want.
ii) add an item to your page e.g. PX_select_column which gets populated from the list of values.
Now for each column you are going to allow a search on, the sql needs to be modified.
Lets pretend that my firstcolumn is called member_id, my second column is called firstname and my third column is lastname
Before we had this SQL
(select * from table where 1=1)
and (PX_SEARCH is null or instr(memberid,PX_SEARCH) > 0 or instr(firstname,PX_SEARCH) > 0 or instr(lastname,PX_SEARCH) > 0 )
Remember we have added PX_SELECT_COLUMN which should contain what we have selected from the LOV.
iii) So now we modify the query to be as follows:
and (PX_SEARCH is null or decode(:PX_SELECT_COLUMN,'MEMBERID',instr(memberid,PX_SEARCH),0) > 0 or
decode(:PX_SELECT_COLUMN,'FIRSTNAME',instr(firstname,PX_SEARCH),0) > 0 or
decode(:PX_SELECT_COLUMN,'LASTNAME',instr(lastname,PX_SEARCH),0) > 0 )
The user can now choose which column to filter their results on.
Here is a basic one ( You can select an object type and put in criteria for the object name like '%b%'. The SELECT statement is dynamically generated with only the pieces of the WHERE clause that are needed. You do not need all the DECODE and INSTR OR logic.
The main part of this example is the computation of the SQL SELECT statement:
  v_sql VARCHAR2(2000) := 'SELECT *'||CHR(10)||'  FROM USER_OBJECTS';
  v_and BOOLEAN := FALSE;
  v_where VARCHAR2(2000);
  -- P1_OBJECT_TYPE has value of 'ALL' for '%'
  IF (:p1_object_type != 'ALL') THEN
    v_sql := v_sql || chr(10) ||
             ' WHERE object_type = '''||:p1_object_type||'''';
    v_and := TRUE;
  IF (:p1_object_name IS NOT NULL) THEN
    IF (v_and) THEN
      v_sql := v_sql || chr(10) ||
               '   AND object_name LIKE UPPER('''||:p1_object_name||''')';
      v_sql := v_sql || chr(10) ||
               ' WHERE object_name LIKE UPPER('''||:p1_object_name||''')';
    END IF;
    v_and := TRUE;
  :p1_sql := v_sql;
  RETURN (v_sql);
Related articles