Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: SQL STATEMENT
On Thu, 27 Aug 1998 01:38:04 GMT, beverett_at_remove_this.usa.net (Brian
Everett) wrote:
>
> Greetings All.
>
> I have a table of zip codes and a web form that attempts to query
>the table based on three fields on the form. The user can provide as
>little or as much information in these three fields as they would
>like. Problem: I do not know in advance which fields will be entered.
>How do I structure the select statement (cursor (Oracle)) so that
>fields not entered will not restrict the returned records but field
>which are entered will restrict the output. I've tried a number of
>approaches and some work well but not in every scenario. Since
>there are three fields then there are 8 possible ways inputs can be
>anticipated. I hope I haven't confused anyone. I used to do something
>in Access like this...
> ...where myfield = myinput or myfield is null;
>
>Oracle needs something else it would appear.
>
You can either
select foo, bar
into localVariable1, localVariable2
from T
where ( myInput1 = col1 or myInput1 is null ) and ( myInput2 = col2 or myInput2 is null ) and ( myInput3 = col3 or myInput3 is null )
3. Use dynamic SQL
eg.
create or replace
procedure getZipCodes( myInput1 varchar2 default null,
myInput2 varchar2 default null, myInput3 varchar2 default null ) isc number;
-- -- remove the trailing ' where' if not inputs exist --
-- -- remove the trailing ' and' --
-- -- Execute the query and process rows --
dbms_sql.parse( c, q, dbms_sql.native ); dbms_sql.define_column( c, 1, localVariable1, 32767); dbms_sql.define_column( c, 2, localVariable2, 32767);s := dbms_sql.execute( c );
-- -- do something --
Hope this helps.
chris.
>Thanks
>Brian
>
>
>Good Luck! Brian_RestonVA
>
>
>
>
Received on Thu Aug 27 1998 - 09:30:01 CDT
![]() |
![]() |