NVL with host variables in a where clause
Does anyone know if using a host variable in a WHERE clause with
the NVL function will actually work if the host variable is null
without using the indicator variable too?
In other words if the variable ":new_prmy_numb" which was
selected previously is actually null, will the NVL know that it
is null without the indicator variable being listed also? The
person at our company who has this in their code says it works
and the rest of us don't see how.
Example:
SELECT recd_a_id from ADDR
WHERE
NVL(prmy_numb,' ') = NVL(:new_prmy_numb,' ')
well firstly have you tried it to see if it works? That would seems to be a rather obvious step, I would imagine.
Anyway, I don't see anything wrong with the code, except that it might be optimized better ...
SELECT recd_a_id from ADDR
WHERE
(prmy_numb = :new_prmy_numb
or (:new_prmy_numb is null and prmy_numb is null))
... might stand more chance of using an index on the column prmy_numb
Similar Messages
-
Using bind variable in dynamic where clause and concatenate with query
Hi,
In my procedure i am framing where clause dynamically with bind variable,When i am concatenate this with my sql query for REF CURSOR i got sql command not properly ended exception.
Is it possible to pass values to the bind variable through the dynamic variable/value?
Please advise
Thanks in advance
Siva
IF in_applicationId IS NOT NULL THEN
optional_where := optional_where || ' AND a.APPLICATION_ID like '||':e%';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id'||',in_applicationId';
END IF;
My query is like this
open Out_Main FOR SelectQuery USING optional_usingThanks for reply,
In my procedure, i suppose to frame the where clause with bind dynamically according to the input parameters. so that i am framing the values of the bind variables also dynamically like this,
Please advise...
IF in_assignedAppFlag IS NOT NULL THEN
IF in_assignedAppFlag = 'Y' THEN
optional_where := optional_where || ' AND b.ASSIGNED_TO = :b' ;
optional_using := ' in_appFuncGroup'||',in_currentUserID';
ELSe
IF in_isSupervisor = 0 THEN
optional_where := optional_where || ' AND (b.ASSIGNED_TO = :b'||' OR b.ASSIGNED_TO = ''-1'' OR b.ASSIGNED_TO IS NULL)';
optional_using := ' in_appFuncGroup'||',in_currentUserID';
END IF;
END IF;
ELSE
IF in_isSupervisor = 0 THEN
optional_where := optional_where || ' AND (b.ASSIGNED_TO = :b'||' OR b.ASSIGNED_TO = ''-1'' OR b.ASSIGNED_TO IS NULL)';
optional_using := ' in_appFuncGroup'||',in_currentUserID';
END IF;
END IF;
IF in_appFuncGroup IS NOT NULL THEN
optional_where := optional_where || ' AND e.APP_FUNC_GROUP= :c';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup';
END IF;
IF in_queue_id IS NOT NULL THEN
optional_where := optional_where || ' AND b.QUEUE_ID = :d';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id';
END IF;
IF in_applicationId IS NOT NULL THEN
optional_where := optional_where || ' AND a.APPLICATION_ID like '||':e%';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id'||',in_applicationId';
END IF;
IF in_sourceCode IS NOT NULL THEN
optional_where := optional_where || ' AND e.APP_SOURCE_CODE like '||':f%';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
||',in_queue_id'||',in_applicationId'||',in_sourceCode';
END IF;
IF in_logo IS NOT NULL THEN
optional_where := optional_where || ' AND appProds.PRODUCT_TYPE like '||':g%';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo';
END IF;
IF in_firstName IS NOT NULL THEN
optional_where := optional_where || ' AND upper(a.FIRST_NAME) like upper(:h%)';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName';
END IF;
IF in_surName IS NOT NULL THEN
optional_where := optional_where || ' AND upper(a.SURNAME) like upper(:i%)';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName'||',in_surName';
END IF;
IF in_retreival_id IS NOT NULL THEN
optional_where := optional_where || ' AND e.RETREIVAL_ID like :j%';
optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName'||',in_surName'||',in_retreival_id';
END IF; -
How to bind a VO :variable in a WHERE clause to a page parameter
Hello,
I have a VO. This VO has a variable :v in the WHERE clause of the query.
This VO is used by different UI components in a page (a table, a tree). The page already has a parameter defined in his properties #{viewScope.myparam}
What I want is to bind :v to the #{viewScope.myparam}, so that I can specify :v from a parameter in the URL, so that the table immeditaly shows the proper results.
Is there a way to Bind :v to #{viewScope.myparam} directly in the VO definition in the model project? (It seems it is not possilble)
The other alternative is to use ExecuteWithParams, but in this case I do not know how to perform the query without user actions in a trasparent way. I dont want to associate execute operations to UI components.
thanksHi,
+Is there a way to Bind :v to #{viewScope.myparam} directly in the VO definition in the model project? (It seems it is not possilble)+
No, this can't be done
The other alternative is to use ExecuteWithParams, but in this case I do not know how to perform the query without user actions in a trasparent way. I dont want to associate execute operations to UI components.
If you are within a bounded task flow or in an unbounded task flow that does not have this page as its first view (home page) then you can drag and drop executeWithParams as a method call activity. The attribute then should be saved in the pageFlowScope (not the viewScope).
In addition I would use a ViewCriteria instead of "hard wiring" the bind variable to the VO definition. You can then - in the AM data model section - associate the View Criteria with the VO instance yo use in the UI
Frank -
Problem with to_date('date_char') in a where clause
A little confused with this plsql error.
This code works...
select to_date(value,'dd/mm/yy') from kip_lov where domain='MODULE START1'
If I try to put the "to_date(value,'dd/mm/yy')" expression in the where clause I get an invalid year error,
select to_date(value,'dd/mm/yy') from kip_lov where domain='MODULE START1'
AND to_date(value,'dd/mm/yy') = SYSDATE
"Error report:
SQL Error: ORA-01841: (full) year must be between -4713 and +9999, and not be 0"
The variable "value" is a varchar2 in the format dd/mm/yy.
Note: the following code also works fine,
select to_date(value,'dd/mm/yy') - sysdate from kip_lov where domain='MODULE START1'
I have modified my lov table so that it uses the date type now and it works fine. Just curious as to why the above example does not work.
Sam.check your table description and values first, is there any dd/mm/yy or mm/dd/yy confliction
A scenario which runs.
SQL> desc trya
Name Null? Type
UPDATED_ON DATE
UPD VARCHAR2(8)
SQL> select * from trya
2 ;
UPDATED_O UPD
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
31-JUL-06 31/07/06
UPDATED_O UPD
31-JUL-06 31/07/06
31-JUL-06 31/07/06
13 rows selected.
SQL> SELECT * FROM trya WHERE TO_CHAR(TO_DATE(upd,'dd/mm/yy'))=SYSDATE;
no rows selected
SQL> SELECT * FROM trya WHERE TO_DATE(upd,'dd/mm/yy')=SYSDATE;
no rows selected
SQL>
NO error on both syntax
rgds,
Rup -
Variable for entire where clause
Is it possible to use a variable for the entire where clause in a PL/SQL select statement?
I have a where clause stored as a string in the database and I want to use that entire string as a variable, but my procedure will not compile because it is missing the relational operator.
SELECT whereclause FROM clausetable WHERE clause = 'TEST'; will return this string: column1 = 'abc'
Here's my procedure:
create or replace
PROCEDURE TESTWC
(c_out OUT sys_refcursor )
AS
BEGIN
DECLARE
v_whereclause VARCHAR2(255);
BEGIN
SELECT whereclause
INTO v_whereclause
FROM clause_table
WHERE clause = 'TEST';
DBMS_OUTPUT.PUT_LINE(v_whereclause);
OPEN c_test FOR SELECT * FROM dpacsoftware WHERE v_whereclause;
NULL;
END;
END TESTWC;Is it possible to use a variable for the entire where clause in a PL/SQL select statement?Unfortunately yes.
I have a where clause stored as a string in the databaseThis is a really bad idea, like storing the source for half your program in a text file with a bunch of Java or C or FORTRAN code and opening, reading, compiling and linking at run time.
It is slow, slow, slow as you can expect, and dangerous and insecure, since someone could have put a format C: command in your source file, or something equivalently nasty to a database. -
How to use a lexical variable in a WHERE clause and as a function argument.
Hello,
My report has a lexical variable which has values like 'and id in (1,3,4)'. I want to use this variable in two separate queries. In the first, it is used in the WHERE clause, something like:
.. where date is not null &var;
In the second query, the variable is used in a function call, something like:
select * from table (my_function(&var));
The trouble is that the first query works when the value of the variable is unquoted, but for the second query the value should be quoted.
Does anyone have an idea how I could solve this problem?
Thanks in advance,
Frans
Message was edited by:
fransiehttp://tkyte.blogspot.com/2006/06/varying-in-lists.html
Presents many options. -
How to use an UNIX shell variable in sql where clause
Hi ,
In my shell script first I get the Date and time from the system into the shell variable.At the end of the shell script I need to run a sql script in which I want to use the variable value in the where clause . Is there any way we can use a shell variable value in SQl script ?. Any help is greatly appriciated .
Thanks in advance,
SampathTry the following
In Unix
SQLPLUS <username>/<password> @MyScript.sql <UNIX_Variable>
In SQL*Plus
Reference the variable as &1
select *
from MyTable
where MyDate = to_date('&1','<date_format>'); -
Reading data from BSEG table with Non-key fields in where clause
Hi All,
I have to read data from BSEG table based on WBS element field (PROJK). As I'm not passing key fields to WHERE clause system couldnt run the select statement. Since BSEG is a cluster table I cant even create secondary index on PROJK field.
Could you please tell me, how to improve its performance.
Regards
Jaker.SELECT bukrs
belnr
gjahr
shkzg
dmbtr
hkont
lifnr
matnr
werks
menge
meins
ebeln
FROM bseg
INTO TABLE it_bseg
PACKAGE SIZE 10
FOR ALL ENTRIES IN it_final
WHERE bukrs EQ it_final-bukrs
AND belnr EQ it_final-belnr
AND gjahr EQ it_final-gjahr
AND buzei EQ it_final-buzei
AND hkont EQ it_final-hkont
AND werks IN s_werks.
By using package and fetch from BSEG table. gathering all other information to a final internal table.This will reduce the hit to database.And also try to put that data in hashed internal table which is it_bseg....then definetly improve the performance.
<REMOVED BY MODERATOR>
Dara.
Edited by: Alvaro Tejada Galindo on Apr 21, 2008 12:47 PM -
Issue with using SHUTTLE in query where clause
Lets say we have a shuttle control on the page with the following 4 values: apple:orange:peach:berry
Lets say I also have a simple SQL query report on the page and I want to use between 1 and 4 of the aforementioned values as part of an "*IN LIST*" sql operation. For example, if the user has selected only apple and orange from the shuttle, I want my query to be:
select * from some_table where fruit IN ('apple', 'orange');
if however, the user has selected all 4 fruits, the query should then read:
select * from some_table where fruit IN ('apple', 'orange', 'peach', 'berry');
I will add the dilemma that I do not want to convert this query to become a PL/SQL based report. Rather, I want to leave it alone as a SQL Query. My initial attempt was to create a hidden item on the page and use APEX_UTIL.STRING_TO_TABLE with a loop to convert the shuttle data to a comma separated list. This worked great, however when I use the this hidden item in my query, it doesn't work properly. I hope I am explaining this well, but when I populate this hidden item, I will get something like this.
apple,orange
Great. Lets say the hidden item above is called P7_HIDDEN_VALUE (for example). I then try to reference it in my query, and here is the problem:
select * from some_table where fruit IN (:P7_HIDDEN_ITEM)
Do you see the problem. The item resolves itself to be apple,orange, when it really needs the quotes to be *'apple','orange'*
Help!!!!Hi,
This post might help
How to enable user to pick which row without using filter in interact. rpt?
Or Bens blog post
http://munkyben.wordpress.com/2010/11/16/handling-aggregated-strings-in-sql-reports/
Regards,
Jari -
Cursor did not return data if using variables in the where clause
ENV: report builder 9.0.4.3.0, DB 10g, OS XP
table and data ctvctyp:
ctvctyp_code ctvctyp_code_pred
CO Z1
FE Z1
FUNCTION get_case_type(case_type VARCHAR2) RETURN VARCHAR2 IS
cursor type_cur is
select ctvctyp_code
from ctvctyp
where ctvctyp_code_pred = :P_case_type; -- if use 'Z1', it will return 'CO','FE'
-- but if 'Z1' is passed in as case_type, it returns null.
temp_type VARCHAR2(4);
return_type VARCHAR2(200);
counter NUMBER;
BEGIN
return_type := '';
counter := 0;
srw.message(20,'in case_type is '||case_type);
open type_cur;
LOOP
FETCH type_cur INTO temp_type;
EXIT WHEN type_cur%NOTFOUND;
counter := counter+1;
if temp_type is NULL then
srw.message(20,'temp type is null');
else
srw.message(20,temp_type);
end if;
if counter = 1 then
return_type := ''''||temp_type||'''';
else
return_type := return_type||','''||temp_type||'''';
end if;
END LOOP;
return return_type;
END;
In my p_casetype validation trigger:
function P_CaseTypeValidTrigger return boolean is
tempCaseType VARCHAR2(200);
return_type VARCHAR2(200);
begin
:P_CASETYPE := 'Z1'
return_type := get_case_type; --- I got null returned.
return (TRUE);
end;
But in sqlplus:
declare a bind variable :P_casetype := 'Z1';
select get_case_type from dual;
returns: 'FE','MI'cursor type_cur is
select ctvctyp_code
from ctvctyp
where ctvctyp_code_pred = _{color:#0000ff}*:P_case_type;*_{color}Shouldn't be this :p_casetype instead of :p_case_type? -
Using a date variable in a where clause
Hi,
I need to run a select query which contains the following
e.g
String query = SELECT APP_DATE, APP_TIME, APP_TYPE, a.CUST_NO
FROM APPOINTMENTS a, PATIENT_DETAILS p
WHERE APP_DATE = " + appDate + " and p.CUST_NO = a.CUST_NO
appData is a java.sql.Date format and contain the date i want to find. The error i get is "Operator = doesn't apply to the types <DATE> and <INT>"
I have also read somewhere that this would return the date at midnight and therefore would not match, how can i fix this. I am using JDataStore for development.
Thanks
Rudyyou want something like this
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
String sql = "SELECT * FROM Table WHERE appDate='" + df.format(appDate) + "';";
Or if you used prepared statements, you could pass the java.sql.Date to it without the need to convert. -
Two variables in the where clause
DECLARE @Id VARCHAR(100)
DECLARE @NO VARCHAR(100)
SET @Id = 'IN'
SET @No = 10
SELECT *
FROM DBName.sys.indexes I
JOIN DBName.sys.tables T ON T.Object_id = I.Object_id
WHERE T.name like '%' +@Id + '_' +@NO+'%'
I need something like the above query that works.Question is vague
Cant understand what you're really looking for as posted query has no obvious errors
Can you give some sample data and explain what you're trying to get as output
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
Trouble with OR in where clause
Hello,
I'm having trouble with execution speed. The problem seems to be with using OR in my where clause.
Here's the meat of the function where i_pledge_number is an input parm:
BEGIN
SELECT /*+ INDEX (pp) */ SUM(pp.prim_pledge_amount)
INTO return_amount
FROM
primary_pledge pp
WHERE
-- Get total if multiple allocations
pp.prim_pledge_number IN
(SELECT pc.pledge_number
FROM pledge_codes pc
WHERE pc.pledge_code_type = 'M'
AND pc.pledge_code = 'AC'
AND lpad(pc.pledge_comment,10,'0') = i_pledge_number)
-- Get total if single allocation
OR pp.prim_pledge_number = i_pledge_number;
RETURN return_amount;
END;
If I comment out either half of the OR statement (either the subquery or the pp.prim_pledge_number = i_pledge_number half) the function returns a value in .02 seconds. If I leave the OR in, it takes 2.764 seconds to execute?? Can someone please show me a better way (faster) to do this? I tried using nvl() around the subquery but couldn't get it to compile.
ThanksThese things are difficult to diagnose remotely, but here is something you can try....
SELECT */ SUM(pp.prim_pledge_amount)
INTO return_amount
FROM primary_pledge pp
WHERE pp.prim_pledge_number IN (SELECT pc.pledge_number
FROM pledge_codes pc
WHERE pc.pledge_code_type = 'M'
AND pc.pledge_code = 'AC'
AND lpad(pc.pledge_comment,10,'0') = i_pledge_number
UNION ALL
SELECT i_pledge_number FROM dual)
RETURN return_amount;
END;If that doesn't do anything (and it might well not) there are a large number of different ways we can recast this query. To save us further guessing please give us more details: execution plans, database version number, volumetrics.
Cheers, APC -
Problem with SYS_CONTEXT in WHERE clause in 10g
I am running the following SELECT statement in 9i and 10g
SELECT COUNT(*)
FROM mapinfo.mapinfo_mapcatalog
WHERE tablename IN ('ESU_GRAPHICS', 'STREETS', 'ASD_STREETS', 'NSG_ARCHIVE_ESU_GRAPHICS')
AND ownername = sys_context('USERENV','CURRENT_SCHEMA');
When running in 9i it returns 4, the correct answer, but in 10g it always returns 0. I have tried populating a variable with the result from sys_context('USERENV','CURRENT_SCHEMA') such as:
SELECT sys_context('USERENV','CURRENT_SCHEMA')
INTO currentschema
FROM dual;
I have checked the variable and it is giving me the correct result in 10g, but I still get 0 returned from the select statement.
What am I missing to get this working in 10g?
Cheers
Sean- Does the value for CURRENT_SCHEMA actually match
the ownername column for the tables in your IN
clause? Yes
>
- Is there anything different between 9i and 10g?
E.g. did you have any fine grained access control?Not that I know of, but I am not a DBA so I could not say for sure.
Were you setting CURRENT_SCHEMA with an ALTER SESSION
statement in 9i that you have missed in your 10g
environment?No we do not use any ALTER SESSION statements.
>
- What do you get if you run the following?
select user from dual;
select sys_context('USERENV','CURRENT_SCHEMA') from
dual;
Exactly the same result
>
- What do you get if you run the following?
SELECT tablename, ownername FROM
mapinfo.mapinfo_mapcatalog
WHERE tablename IN ('ESU_GRAPHICS', 'STREETS',
'ASD_STREETS', 'NSG_ARCHIVE_ESU_GRAPHICS');
380 rows of data including the 4 that I am interested in
- Can you also show the value of your variable when
you use a SELECT INTO in PL/SQL... Were you using
uppercase for USERENV and CURRENT_SCHEMA in PL/SQL
(there was an incorrect results bug when using
lowercase attributes in PL/SQL SYS_CONTEXT calls)?I am showing the value of the variable and it is holding the correct value, and yes I am using uppercase USERENV and CURRENT_SCHEMA.
After more investigation it looks like it is not SYS_CONTEXT, it is using a variable in the WHERE clause. If I set the variable to a hard coded value I still get the same result. If I replace the variable for a hard-coded string in the WHERE clause I get the correct result. -
Pagination after executequery with a dynamic where clause
Hi all,
I have a problem with pagination after setting a where clause dynamically and performing an executequery in ViewObjectImpl.java.
As a result of this, the amount of records are decreased returned by the viewobject, however the pagination is not changed. It still reflects the previous amount. It changes only if you have selected the last range of the changed recordset
Does anybody know how to get the pagination to refect the correct amount of the updated recordset of the viewobject?
Jdeveloper version 10.1.3.4 / Jheadstart 10.1.3.3.81
Thanks in advance, ErwinHi,
I would not add this to the VO query. Instead I would create a View Criteria that uses a bind variable. Then in the Application Module Data Model, you select the VO instance and choose edit (button on top) to permanently associate the view criteria)
Advantages of this approach
- The VO definition can be used elsewhere without the restriction
- the bind variable is not mandatory and yu can set it such that if no value is provided a full query is executed
Frank
Maybe you are looking for
-
Sharing Mac with two iTunes accounts
Recently bout an Mac air In our family we have one Mac but two accounts for iTunes Can my wife add her iTunes library to guests without hurtin either her library on her iPod or my library on the Mac? Thanks
-
Synchronizing Updates on a Staging Table
Please help me out with the resolving the following issue: A load script is running for moving records from a data file to a staging table. After this script completes, there is a code to update two fields of the staging table. To do this the shell s
-
LR Causes Softness on Corner of Image
I have a very strange problem and am wondering if anybody else has seen this. I am duplicating a painting using my Canon 5Dmk2 and some studio lights. I mounted the 22x28 inch painted canvas to a flat wall. I then mounted my camera on a tripod, perpe
-
This might have something to do with recent Mac software updates but I can no longer print pics in '08. I get the message "No available themes" "until at least one theme has been installed this feature (printing) is not available" How did this happen
-
Image won't print at the designated size
I have the following: CS4 (brand new) Windows 7 (64 bit) New HP office jet 8500 wireless Problem: I complete an image. Image size is set to 8.5 x 11. Print preview shows image filling paper. Image is set to print at 100%. When I press