SELECT SYS_CONTEXT ('userenv', 'ip_address') FROM dual;
I am using the following query
SELECT SYS_CONTEXT ('userenv', 'ip_address') FROM DUAL:
and the retuned row is blank.
Any Idea why?
SELECT SYS_CONTEXT ('userenv', 'ip_address') FROM dual
will return the IP address of the client of the current session. If the current session is connected to a local database (i.e. not using SQL*NET), then this query appears to return NULL.
SELECT SYS_CONTEXT ('userenv', 'host') FROM dual
will return the host name from which the client is connected, even if the client is connected locally.
If the host name is not sufficient, then I'm not sure what your other options are (perhaps writing a java stored procedure which returns the IP address of the database server? always connecting via SQL*Net?)
Similar Messages
-
hi master
sir i use this command for user ip address
SELECT sys_context('userenv', 'host'), sys_context('userenv', 'ip_address') FROM dual;
BUT
form6i give me this error
Error 201 at line 26, column 12
Identifier SYS_CONTEXT must be declared
please give me idea how i get ip_address in form
thankthank for your reply
sir my need is
i check user computer name if that computer in table then i update the user feeding ouer wise i insert that computer name as new and give the feeding form access to user
you see my code
SELECT sys_context('userenv','host'), sys_context('userenv', 'ip_address') INTO HOSTNAME,IPADD FROM dual;
SELECT COUNT(*) INTO USERCOUNT FRoM forclosingyear WHERE HOSTNAME=HOSTNAME AND IPADD=IPADD;
IF USERCOUNT>0 THEN
update forclosingyear SET YEARID=:SYID,datefrom=:ysdate,dateto=:yedate WHERE HOSTNAME=HOSTNAME AND IPADD=IPADD;
ELSIF USERCOUNT=0 THEN
INSERT INTO forclosingyear (YEARID,datefrom,dateto,HOSTNAME,IPADD) VALUES (:SYID,:ysdate,:yedate,HOSTNAME,IPADD);
END IF;
this is my need
please give me idea how i get termenal name or user computer name
thank
aamir -
Problem with: select 'c' as X from dual
Problem with 'select 'c' as X from dual'
I get 2 different results when I execute the above with SQLPlus (or java) depending on the instance I am connected to. For one instance the result is a single character and for the other the character is padded with blanks to 32 chars in the SQLPlus window (and java). Does anyone know what database setting causes this to happen? Is it a version issue ?
Test #1: Oracle 9.2.0.6 - SQLPlus result is padded with blanks
SQL*Plus: Release 9.2.0.1.0 - Production on Mon Dec 10 09:27:58 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
With the Partitioning option
JServer Release 9.2.0.6.0 - Production
SQL> select 'c' as X from dual;
X
c
SQL>
Test #2 Oracle 9.2.0.1 SQLPlus result is a single character.
SQL*Plus: Release 9.2.0.1.0 - Production on Mon Dec 10 09:29:27 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to:
Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
SQL> select 'c' as X from dual;
X
c
SQL>Using 9.2.0.6 On AIX 5.2 I get the single byte result:
UT1 > select 'c' as X from dual;
X
c
If the databases are on different Oracle Homes you may want to check the sqlplus global logon files for any set commands.
If you executed the two sql statements from different OS directories you may also want to check your sqlpath for sqlplus .logon files.
Try issueing clear columns and repeating the statement. Are the results the same?
HTH -- Mark D Powell -- -
Hi GURUS,
I m new to oracle here i created a table in oracle 10g(10.1.0.2.0) named
create table iv_master(
iv_code varchar2(6) not null,
iv_name varchar2(50) not null,
created_at varchar2(15) default substr(sys_context('userenv','ip_address'),1,20),
created_on date default sysdate,
created_by varchar2(5) not null,
modified_at varchar2(15) default substr(sys_context('userenv','ip_address'),1,20),
modified_on date default sysdate,
modified_by varchar2(5) not null,
constraint iv_master_pk primary key(iv_code))
Now i want to remove this 'default' keyword in created_at & modified_at fields
i tried alter table command but it is not working.tell me the way to remove this default keyword and also in created_on ,modified_on..(without dropping the table)
i tried the following statement,
create table test (ipaddr varchar2(20) SUBSTR(SYS_CONTEXT('USERENV','IP_ADDRESS')1,20));
but i m getting.this following error
ERROR at line 1:
ORA-00922: missing or invalid option
pls tell me where i m wrong...and also tell me the query to do it...
Advance thanks for all...Hi,
bharathit wrote:
Hi GURUS,
I m new to oracle here i created a table in oracle 10g(10.1.0.2.0) named
create table iv_master(
iv_code varchar2(6) not null,
iv_name varchar2(50) not null,
created_at varchar2(15) default substr(sys_context('userenv','ip_address'),1,20),
created_on date default sysdate,
created_by varchar2(5) not null,
modified_at varchar2(15) default substr(sys_context('userenv','ip_address'),1,20),
modified_on date default sysdate,
modified_by varchar2(5) not null,
constraint iv_master_pk primary key(iv_code))
Now i want to remove this 'default' keyword in created_at & modified_at fieldsYou can't have the default functionality without the "DEFAULT" keyword.
Are you saying that you no longer want a default value? That's the same as saying DEFAULT NULL, and you can change the table like this:
ALTER TABLE iv_master
MODIFY ( created_at DEFAULT NULL
, modified_at DEFAULT NULL
);Edited by: Frank Kulash on Feb 17, 2010 12:59 PM
Changed ALTER TABLE example to modify both columns. -
Does anyone have a list of all of the environment selections available from dual.
e.g. select user from dual;
Thanks in advance
BobHi Bob,
there are a lot of usful selections possible using DUAL. For more
information I'd like to suggest you to look into SQL Reference Guide
chapter 4, which describes for example the powerful SYS_CONTEXT
function.
Regards,
Roland
Find attached some usful examples on using dual:
================================================
Current session:
================
SQL> select sid,serial#
from v$session
where audsid in (SELECT USERENV('SESSIONID') FROM DUAL);
SID SERIAL#
13 830
Which protocol is currently used for connection?
================================================
SQL> SELECT SYS_CONTEXT('USERENV','NETWORK_PROTOCOL') from dual;
SYS_CONTEXT('USERENV','NETWORK_PROTOCOL')
tcp
When nothing will be returned then you're using the BEQUEATH protocol.
(Session on loacal server that has connected without using a listener)
Which IP address will be used by session?
=========================================
SQL> SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') "My IP Address" from dual;
My IP Address
140.84.134.14
Which NLS Environment will be used?
===================================
SQL> SELECT SYS_CONTEXT('USERENV','LANGUAGE') "nls_lang" from dual;
nls_lang
AMERICAN_AMERICA.WE8ISO8859P1 -
Parameter in SYS_CONTEXT('USERENV'
Hi,
is there any parameter in SYS_CONTEXT('USERENV' that shows the name or IP adresse of database server ( machine on which DB is running) ?
many thanks.
I know the followings but non lets know (but if I'm wrong).
SYS_CONTEXT('USERENV','TERMINAL') terminal,
SYS_CONTEXT('USERENV','LANGUAGE') language,
SYS_CONTEXT('USERENV','SESSIONID') sessionid,
SYS_CONTEXT('USERENV','INSTANCE') instance,
SYS_CONTEXT('USERENV','ENTRYID') entryid,
SYS_CONTEXT('USERENV','ISDBA') isdba,
SYS_CONTEXT('USERENV','NLS_TERRITORY') nls_territory,
SYS_CONTEXT('USERENV','NLS_CURRENCY') nls_currency,
SYS_CONTEXT('USERENV','NLS_CALENDAR') nls_calendar,
SYS_CONTEXT('USERENV','NLS_DATE_FORMAT') nls_date_format,
SYS_CONTEXT('USERENV','NLS_DATE_LANGUAGE') nls_date_language,
SYS_CONTEXT('USERENV','NLS_SORT') nls_sort,
SYS_CONTEXT('USERENV','CURRENT_USER') current_user,
SYS_CONTEXT('USERENV','CURRENT_USERID') current_userid,
SYS_CONTEXT('USERENV','SESSION_USER') session_user,
SYS_CONTEXT('USERENV','SESSION_USERID') session_userid,
SYS_CONTEXT('USERENV','PROXY_USER') proxy_user,
SYS_CONTEXT('USERENV','PROXY_USERID') proxy_userid,
SYS_CONTEXT('USERENV','DB_DOMAIN') db_domain,
SYS_CONTEXT('USERENV','DB_NAME') db_name,
SYS_CONTEXT('USERENV','HOST') host,
SYS_CONTEXT('USERENV','OS_USER') os_user,
SYS_CONTEXT('USERENV','EXTERNAL_NAME') external_name,
SYS_CONTEXT('USERENV','IP_ADDRESS') ip_address,
SYS_CONTEXT('USERENV','NETWORK_PROTOCOL') network_protocol,
SYS_CONTEXT('USERENV','BG_JOB_ID') bg_job_id,
SYS_CONTEXT('USERENV','FG_JOB_ID') fg_job_id,
SYS_CONTEXT('USERENV','AUTHENTICATION_TYPE')
authentication_type,
SYS_CONTEXT('USERENV','AUTHENTICATION_DATA')
authentication_data,
SYS_CONTEXT('USERENV','CURRENT_SQL') current_sql,
SYS_CONTEXT('USERENV','CLIENT_IDENTIFIER') client_identifier,
SYS_CONTEXT('USERENV','GLOBAL_CONTEXT_MEMORY')We have 10g and 9i DBs. We should be able to know to which machine user is connected.
Thank you.
On 9i I tested :
SQL> select SYS_CONTEXT('USERENV','hostname') from dual;
select SYS_CONTEXT('USERENV','hostname') from dual
ERREUR à la ligne 1 :
ORA-02003: invalid USERENV parameter -
Sys_context not working from APEX application / environment
I am struggling to retrieve a user-defined context value using sys_context(...) from APEX application / environment
I have created a global context to be used as a storage mechanism for log severity to be recorded.
CREATE OR REPLACE CONTEXT PA_LOGGER_CTX USING PA_LOGGER ACCESSED GLOBALLY;It is seeded through a function pa_logger.set_severity(1) within the package pa_logger through (excerpt):
dbms_session.set_context(c_ctx_context_label, c_ctx_severity_label, TO_CHAR(p_severity_in));
with:
c_ctx_context_label CONSTANT NVARCHAR2(16) := 'PA_LOGGER_CTX';
c_ctx_severity_label CONSTANT NVARCHAR2(8) := 'SEVERITY';
p_severity_in IN PLS_INTEGERusing the following from sqlplus, other DB-tools, works as expected:
-- setting it
BEGIN
pa_logger.set_severity(2);
END;
-- displaying it
SELECT sys_context('PA_LOGGER_CTX', 'SEVERITY') FROM DUAL;returns 2
This works from different sessions, different users ... no problem.
Using it from within an APEX (accessed via embedded gateway/XE11.2 or OHS/11.2.0.2.0 ) application or even from the "SQL Commands" from the APEX Workspace returns a NULL value for :
SELECT sys_context('PA_LOGGER_CTX', 'SEVERITY') FROM DUAL;And
SELECT value FROM GLOBAL_CONTEXT WHERE NAMESPACE='PA_LOGGER_CTX' and ATTRIBUTE = 'SEVERITY';returns the right value, which obviously defies the purpose of using the CONTEXT.
It is as well possible to set the CONTEXT to a different value, which is picked up outside APEX, but even so using sys_context does not work, always returning NULL
As a note aside: using sys_context with a standard CONTEXT, e.g. SELECT sys_context('USERENV', 'NLS_DATE_FORMAT') from DUAL works without any problems.
I guess I have a conceptual break somewhere. Does this have to do with the APEX user used, wrapping within the APEX environment, the way the context is defined ....
Any help is appreciated.
Many thanks,
- ThomasHi Rod,
Thanks for the time to answer. Meanwhile I found some more hints on the subject (Retrieve values from my own global context in Apex and I do now understand the concept.
I understand that I can even clear the specific CLIENT_IDENTIFIER by issuing a "dbms_session.clear_identifier()", which then subsequently works
Looking at the CLIENT_IDENTIFIER as set by APEX it is "<apex_login_user><apex_session>";
I understand it is set for every DISPLAY-SUBMIT it is reset by the APEX framework.
Hence it is not sufficient to set it for a user session; it needs to be changed for every display-submit. I can achieve this through application processes, but I am really reluctant to do so, because I do not know, if APEX relies on the CLIENT_IDENTIFIER to be set the way it is done. I suspect a purpose behind this, causing me problems elsewhere. I post this as a seprate question as this question has been answered.
On to your point about: Using a table variable. This is the way it works currently, but allegedly using the CONTEXT would give us for the logging configuration a performance gain of about 1 dimension..... but unfortunately other problems..
Thanks!
- Thomas -
Hi ,
I am using below query in my procedure. Can it possible below query give NULL. If yes then which Case...?
SELECT SYS_CONTEXT('USERENV','OS_USER')
FROM DUAL
Thanks lot..
Edited by: user8568838 on Mar 1, 2011 2:05 AM
Edited by: user8568838 on Mar 1, 2011 2:27 AMBluShadow wrote:
smon wrote:
Please apologize and remove the word 'Urgent'.lol, lighten up!It's not about lightening up, it's about people coming here and being rude by assuming everyone should drop what they're doing to help them "urgently". You would consider it rude if you were busy talking to people in a room and someone came in demanding your urgent attention without consideration for what you're doing.
I've now edited the subject line to remove the "urgent" from it.That isn't the intention, to get everyone to drop what they're doing. Marking it as urgent just means the clocks ticking at his end, he isn't haughtily making demands of me, you or anyone else.
The meeting room analogy doesn't work, as the poster isn't barging in and disturbing us from whatever else we're doing. He's passively posting on a forum, and we're actively looking for his, or anyone elses posts, to read and reply to. If I'm busy doing something else, then the poster with the urgent problem isn't disturbing me, because I'm not on the forum. -
Not matching the SYS_CONTEXT('USERENV', 'LANG') with oracle fod schema data
I have imported the fod schema into oracle XE. I observed that the data available for following languages:
SQL> select distinct language from category_translations;
LANGUAGE
EN
EL
FR
DE
JA
But, when I check the value for the LANG in oracle XE, it is returning as 'US'.
SQL> select SYS_CONTEXT('USERENV','LANG') FROM DUAL;
SYS_CONTEXT('USERENV','LANG')
US
Becasue of this, I am not getting data when I run VOs from JDeveloper. Even I am not able to see data from FOD schema views.
Please suggest what changes I need to do for getting data.Try this,it will work
alter session set nls_language = 'GREEK'
If the above query is used,it will set the current session's language code to 'GL' which is valid in the language filed of category_translations.
I did not find any nls_language value to get the 'EN' code.
You can get the valid language values from the below query:
select * from V$NLS_VALID_VALUES where parameter='LANGUAGE'
Thanks,
Mohan -
Select from dual into a variable through db links
HI,I need to select value of current_timestamp in to a variable d1 of a remote database inside function.
function (db_lnk varchar2)return number
as
dbl varchar2(10);
begin
dbl:=db_lnk;
select current_timestamp into d1 from dual@dbl;
end;
but getting error table or v iew does not exist.
if i do
select current_timestamp into d1 from dual@'||dbl||';
then it says database link name expected.
How to achieve this?
ThanksPeter Gjelstrup wrote:
Foreign languages, foreign languages :-){noformat}*grins*{noformat} I know - and your English is miles better than my Danish (I'm assuming - hopefully correctly?! - that that's your 1st language based on your location!) which I don't even know any words of! *{:-) -
Hi,
I have the problem with the new managed ODP.Net driver.
If I fetch data from a select which has column with a fixed value "NULL" the driver decides after a certain amount of data that the value is not null any more. Which causes a lot of problems especially if you try to hide not needed blob data.
The Problem somehow depends on the FetchSize of the command. It seems like the error occurs if more than one db round trip to fetch the result is necessary.
System: Windows 7 64 Bit
Platform: .net 4.0 x86
database: 11g Release 11.2.0.3.0 - 64bit Production
Oracle.ManagedDataAccess Version: 4.112.350
I created a small example to reproduce the problem.
Thanks for your help
Dominik
Stored Proc:
create or replace PROCEDURE TestNullField
v_IntPara IN NUMBER DEFAULT NULL ,
v_StrPara IN VARCHAR2 DEFAULT NULL,
cv_1 OUT SYS_REFCURSOR
AS
BEGIN
OPEN cv_1 FOR
select rownum, v_StrPara, NULL from dual connect by level <= v_IntPara;
--select IDX, NULL, DESCRIPTION FROM TEST_BLOBTABLE;
END;C# Code:
using System;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using System.Data;
namespace OracleBlobTest
class Program
private static string _connectionString = @"User ID=YourUser;Password=YourPwd;Data Source=YourServer:YourPort/YourSchema;";
private static string _spName = @"TestNullField";
private static string _strPara = @" Long test string";
private static int _intPara = 200;
static void Main(string[] args)
using (OracleConnection connection = new OracleConnection(_connectionString))
using (OracleCommand cmd = connection.CreateCommand())
cmd.CommandText = _spName;
cmd.CommandType = CommandType.StoredProcedure;
connection.Open();
string alongString = _strPara;
while (alongString.Length < 2000)
alongString += alongString;
alongString = alongString.Substring(0, 2000);
OracleCommandBuilder.DeriveParameters(cmd);
if (cmd.Parameters.Count > 0 && (cmd.Parameters[0]).Direction != ParameterDirection.ReturnValue)
cmd.Parameters[0].Value = _intPara;
cmd.Parameters[1].Value = alongString;
// change this to change the moment when it starts to go wrong
///cmd.FetchSize = 5000;
using (OracleDataReader reader = cmd.ExecuteReader())
int count = 0;
while (reader.Read())
count++;
for (int idx = 0; idx < reader.FieldCount; idx++)
if (reader.GetName(idx) == "NULL")
if (!reader.IsDBNull(idx))
//something is very wrong here - why is not not null any more???
Console.WriteLine("Fix NULL Field[{0}] {1} is not null >{2}< in row {3} ", idx, reader.GetName(idx), reader[idx], count);
Console.WriteLine("Rows found: " + count);
connection.Close();
Console.WriteLine("done press enter");
Console.ReadLine();
}Edited by: user540519 on 10.12.2012 15:11
Edited by: user540519 on 19.12.2012 13:50Hello
I ran the testcase here and reproduced the issue on 32 bit unmanaged beta v4.0.30319
note: same testcase works with Oracle.DataAccess (but not with managed).
This appears to match unpublished defect Bug 14666093 and is meant to be fixed in a later beta release.
Some things I noticed when testing
with the default fetchsize the breaking point is 67 iterations.
e.g.
private static int _intPara = 66; // Works
private static int _intPara = 67; // Fails
If I increase the fetchsize then it breaks at different values as you noticed..
Hope this helps.
Kind Regards
John -
Function in Select...from dual
Hi
One generate question.
We can do the following task in the Select Statements:-
1. Select Function_Name('Parameter) from dual;
2. Select Package_Name.Function_Name('Parameter') from dual;
Why dont we can call the procedure from the Select Statements:-
Select Procedure_Name('Parameter') from dual;
Can we we call the Procedure in side the procedure statements ???
Thanks
SandeepProcedure or function, it's not an Oracle question. In all prog language, it's same.
Where a function return one value into a variable (or query here) :
declare
var_in1 varchar2(10);
var_in2 varchar2(10);
var_out varchar2(10);
var_out:=myfunction(var_in1,var_in2);
print var_out;
--or
print myfunction(var_in1,var_in2);a procedure have some output variable (may be more than one) :
declare
var_in1 varchar2(10);
var_in2 varchar2(10);
var_out1 varchar2(10);
var_out2 varchar2(10);
Myprocedure(var_in1,var_in2,var_out1,var_out2);
print var_out1;
print var_out2;Nicolas. -
Why a function within a SELECT FROM DUAL is faster?
1)I have a SELECT with a function. Ex:
"SELECT DISTINCT function(...) FROM table"
2)I changed the SELECT and put the function inside a SUB-SELECT with FROM DUAL. Ex:
"SELECT DISTINCT (SELECT function(...) FROM DUAL) FROM table"
3)The second SELECT is faster than the first.
I discovered this problem on my tables and my functions. So I did a generic example for this forum with a common function and large table. My results:
Table has 2.196.058 records and the field is a VARCHAR2:
SELECT DISTINCT SUBSTR(field, 2) FROM Table -> Executed in 110 seconds
SELECT DISTINCT (SELECT SUBSTR(field, 2) FROM DUAL) FROM Table -> Executed in 39 seconds
Why "SELECT DISTINCT (SELECT function(...) FROM DUAL) FROM table" is faster than "SELECT DISTINCT function(...) FROM table"
thanks,
FernandoHi hoek,
I followed your informations and, sorry, I am more confused! I did an interesting example below.
1)I created a table:
CREATE TABLE T
name VARCHAR2(50)
); 2)I inserted 10 lines:
NAME
A
B
C
D
E
F
G
H
I
J 3)I created a function that returns the parameter concatenated with the current millisecond:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END; 4)The query(A): SELECT f(name) AS col1, f(name) AS col2, f(name) AS col3 FROM t; gave me:
COL1 COL2 COL3
A-693786000 A-693887000 A-693941000
B-693989000 B-694017000 B-694043000
C-694071000 C-694097000 C-694124000
D-694153000 D-694180000 D-694206000
E-694235000 E-694261000 E-694287000
F-694316000 F-694341000 F-694367000
G-694396000 G-694422000 G-694448000
H-694477000 H-694503000 H-694529000
I-694557000 I-694583000 I-694609000
J-694638000 J-694664000 J-694690000If I repeat the SELECT, new values are generated.
4)The query(B): SELECT (SELECT f(name) FROM Dual) AS col1, (SELECT f(name) FROM Dual) AS col2, (SELECT f(name) FROM Dual) AS col3 FROM t; gave me:
COL1 COL2 COL3
A-253546000 A-253643000 A-253746000
B-253791000 B-253821000 B-253850000
C-253881000 C-253909000 C-253937000
D-253969000 D-253997000 D-254026000
E-254057000 E-254085000 E-254113000
F-254145000 F-254173000 F-254202000
G-254232000 G-254261000 G-254289000
H-254320000 H-254348000 H-254376000
I-254407000 I-254436000 I-254464000
J-254495000 J-254523000 J-254551000The result generated new values too.
5)I changed the function and I put the DETERMINISTIC clause:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR DETERMINISTIC IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END; 6)I repeated the queries(A) and (B) and the result has generated different values. Then, with the DETERMINISTIC clause, the result has not changed
7)I changed the function and I put the RESULT_CACHE clause:
CREATE OR REPLACE FUNCTION f (param1 IN VARCHAR2) RETURN VARCHAR RESULT_CACHE IS
BEGIN
return param1 || '-' || to_char(CURRENT_TIMESTAMP, 'FF');
END;8)I repeated the query(A) and now, the result was different:
COL1 COL2 COL3
A-381048000 A-381048000 A-381048000
B-381242000 B-381242000 B-381242000
C-381322000 C-381322000 C-381322000
D-381400000 D-381400000 D-381400000
E-381481000 E-381481000 E-381481000
F-381556000 F-381556000 F-381556000
G-381634000 G-381634000 G-381634000
H-381815000 H-381815000 H-381815000
I-381895000 I-381895000 I-381895000
J-381971000 J-381971000 J-381971000The line columns are equals and if I repeat the SELECT, all the result is the same too. The query(B) returned the same result like the query(A)! Now I see the cache in action!
After the tests, I concluded that the function in the subquery doesn't use cache and it is faster than the normal function. The mistery continues... -
Strange "select level from dual connect by level :N" behavior
Oracle SQL Deveoper version:1.1.0.23
Oracle database server version:
select * from v$version;
Oracle9i Enterprise Edition Release 9.2.0.7.0 - Production
PL/SQL Release 9.2.0.7.0 - Production
"CORE 9.2.0.7.0 Production"
TNS for 32-bit Windows: Version 9.2.0.7.0 - Production
NLSRTL Version 9.2.0.7.0 - Production
Run the following queries in sequence as a script and look at number of rows returned:
select 1, level l from dual connect by level < 1;
select 2, level l from dual connect by level < 2;
select 3, level l from dual connect by level < 3;
select 4, level l from dual connect by level < 4;
select 5, level l from dual connect by level < 5;
select 6, level l from dual connect by level < 6;
select 7, level l from dual connect by level < 7;
select 8, level l from dual connect by level < 8;
select 9, level l from dual connect by level < 9;
select 10, level l from dual connect by level < 10;
select 11, level l from dual connect by level < 11;
select 12, level l from dual connect by level < 12;
1 L
1 1
1 rows selected
2 L
2 1
1 rows selected
3 L
3 1
3 2
2 rows selected
4 L
4 1
4 2
4 3
3 rows selected
5 L
5 1
5 2
5 3
5 4
4 rows selected
6 L
6 1
6 2
6 3
6 4
6 5
5 rows selected
7 L
7 1
7 2
7 3
7 4
7 5
7 6
6 rows selected
8 L
8 1
8 2
8 3
8 4
8 5
8 6
8 7
7 rows selected
9 L
9 1
9 2
9 3
9 4
9 5
9 6
9 7
9 8
8 rows selected
10 L
10 1
10 2
10 3
10 4
10 5
10 6
10 7
10 8
10 9
9 rows selected
11 L
11 1
11 2
11 3
11 4
11 5
11 6
11 7
11 8
11 9
11 10
10 rows selected
12 L
12 1
12 2
12 3
12 4
12 5
12 6
12 7
12 8
12 9
12 10
10 rows selected
1000 L
1000 1
1000 2
1000 3
1000 4
1000 5
1000 6
1000 7
1000 8
1000 9
1000 10
10 rows selected
-- wrong result. return 1 row, should return 0 row
-- May have some automagical code saw dual and return 1 row.
select 1, level l from dual connect by level < 1;
-- wrong result. Should return 11 rows, but it stopped at 10 rows.
select 12, level l from dual connect by level < 12;
select 1000, level from dual connect by level < 1000;""By default, when Oracle JDBC executes a query, it receives the result set 10 rows at a time from the database cursor.""
Sql dev overrides the default and fetches 50 (1.0) or 100 (1.1) rows at a time. In 1.1 this is configurable.
"I don't HATE any SQL, as long as it's valid."
That statement is absurd. So, if someone tells you a way to write your query that it allows it to run faster, you wouldn't care? You should hate some SQL, some of it is pure crap and needs to be rewritten. This is what we call 'junk code'. As a developer, your goal should be to write maintainable code. In short, if you have the choice between something that looks fancy and something that is legible and maintainable, choose the maintainability. Sorry, but if I ever saw that statement in a code review I would have made you rewrite it. Also, anything that runs differently on different databases should be avoided, its that simple. -
Query Issue with select level from dual
Hi,
I have a question regarding this query. The problem seems that when selecting level from dual while including another table the rows returned seem to increase exponentially.
I can add distinct and get the correct number of rows but am worried that this will cause a possible performance issue. Am I using the level option wrong?
I have included details below.
There are 4 rows in tbl_incidents
When I run the following queries I get rows returned based on the total number of rows
select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
where incident_id = 6
connect by level <= 1;
returns 1 row
select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
where incident_id = 6
connect by level <= 2;
returns 5 rows
select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 3 and incident_id = 6;
returns 21 rows
select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 4 and incident_id = 6;
returns 85 rows
select start_date + level - 1, tbl_incidents.incident_id, level
from dual, tbl_incidents
connect by level <= 5 and incident_id = 6;
returns 341 rows
So with
r being the number of rows in tbl_incidents and
l being the number used in the connect by for level and
q being the number of rows returned by the query
it appears that
q(l) = r * q(l-1) + 1
level 2: 4 * 1 + 1 = 5
level 3: 4 * 5 + 1 = 21
level 4: 4 * 21 + 1 = 85
level 5: 4 * 85 + 1 = 341
Thanks much,
NoraHi,
The dual table is used when you want to do something in SQL when you are not otherwise using a table.
Generating a "counter table" of the integers 1, 2, 3,..., X is an example
SELECT LEVEL AS n
FROM dual
WHERE LEVEL <= x;There is never any point in joining dual to another table, as in
select start_date + level - 1
, tbl_incidents.incident_id
, level
from dual
, tbl_incidents
where incident_id = 6
connect by level <= x;You will always get the same more easily by just eliminating dual:
select start_date + level - 1
, incident_id
, level
from tbl_incidents
where incident_id = 6
connect by level <= x;It is quite useful and common to join a counter-table to a real table, like this cross-join:
WITH counter_table AS
SELECT LEVEL AS n
FROM dual
WHERE LEVEL <= x
select start_date + n - 1
, incident_id
, n
from tbl_incidents
, counter_table
where incident_id = 6
Maybe you are looking for
-
i tried downloading itunes 10.5 and it fails. so i tried downloading it manually. at the end of the installation it says invalid drive G:/ and then it stops. i do not have a drive g anyone have suggestions.
-
Pictures emailed from my Mac on Iphoto 08 are too small even though I sent them 'actual size.' Sent as 5.7MB, received as 151KB.
-
How do I get HP to comp me for cyan color cartridge that quit working? CP1215
Two months ago I purchased a new cyan ink cartridge. The printer had been working fine until a couple of days ago. I got this error message: catridge memory error on cyan. It says for me to replace the cartridge. This is for HP Color LaserJet CP1215.
-
Hi, I am new to java, and was looking for some java code to accomplish the following: I have an XML file, and a target XML schema. I also have a mapping schema, that maps the source xml file attributes with the target schema attributes. I want to rea
-
Hi since iCal (2.x used in Tiger) doesn't have CalDav support, im forced to use SunBird from Mozzila for all my Tiger and XP Machines (Leopard Machines work fine). I have 4 groups created, all have their own calendars. I've managed to get SunBird wor