Want to se Arithmetic Operators Dynamically [message #386039] |
Thu, 12 February 2009 05:22 |
sanyadu
Messages: 30 Registered: October 2006 Location: Chicago
|
Member |
|
|
Hi ,
I have to select certain values from a table based on some condition, but till run time i don't know the arithmetic condition,
The test query looks like
select name , salary
from employees
where salary > 1000;
Now in place of > i could get anything and based on that i have to use it in where clause, if i get '<>' then my where clause will be
where salary <> 1000;
Please suggest how would i implement it my code.
Thanks.
|
|
|
|
|
|
|
Re: Want to se Arithmetic Operators Dynamically [message #386045 is a reply to message #386043] |
Thu, 12 February 2009 06:01 |
sanyadu
Messages: 30 Registered: October 2006 Location: Chicago
|
Member |
|
|
I have created below function
CREATE OR REPLACE FUNCTION get_num_of_employees (loc VARCHAR2)
RETURN NUMBER IS
query_str VARCHAR2(1000);
num_of_employees NUMBER;
BEGIN
query_str := 'SELECT COUNT(*) into INTO num_of_employee FROM '
|| ' employees '
|| ' WHERE rule_nbr '|| loc || ' 1 ';
EXECUTE IMMEDIATE query_str;
RETURN num_of_employees;
END;
And call it in external environment
declare
a number;
begin
a:= get_num_of_employees('=');
dbms_output.put_line(a);
end;
but i will get Missing expression error
|
|
|
|
|
|
|
Re: Want to se Arithmetic Operators Dynamically [message #386201 is a reply to message #386054] |
Fri, 13 February 2009 02:08 |
sanyadu
Messages: 30 Registered: October 2006 Location: Chicago
|
Member |
|
|
Sure here is the sql :-
declare
lv_char VARCHAR2(10) := '=' ;
lv_sql VARCHAR2(100);
lv_val_name VARCHAR2(14);
strsql VARCHAR2(100);
strsqlwher VARCHAR2(100);
strfinal VARCHAR2(100);
begin
strsql := ' Select value_type_name from value ' ;
strsqlwher := ' WHERE value_id ' || lv_char || 13286 ;
strfinal := strsql || strsqlwher;
execute immediate strfinal into lv_val_name ;
dbms_output.put_line(' Final query is '|| strsql || strsqlwher);
dbms_output.put_line( 'lv_val_name is ' || lv_val_name);
end;
And below is the Procedure
CREATE OR REPLACE FUNCTION get_num_of_employees (loc VARCHAR2)
RETURN NUMBER IS
query_str VARCHAR2(1000);
num_of_employees NUMBER;
BEGIN
query_str := 'SELECT COUNT(*) FROM '
|| ' rule '
|| ' WHERE rule_nbr '|| loc || 1 ;
EXECUTE IMMEDIATE query_str INTO num_of_employees ;
RETURN num_of_employees;
END;
declare
a number;
begin
a:= get_num_of_employees('>');
dbms_output.put_line(a);
end;
Thanks
Sandeep
[Updated on: Fri, 13 February 2009 02:13] by Moderator Report message to a moderator
|
|
|