﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Martin's Home-文章分类-Oracle</title><link>http://www.blogjava.net/martinyuan/category/39731.html</link><description>希望我的积累对大家有所帮助！</description><language>zh-cn</language><lastBuildDate>Thu, 21 May 2009 09:18:04 GMT</lastBuildDate><pubDate>Thu, 21 May 2009 09:18:04 GMT</pubDate><ttl>60</ttl><item><title>Oracle 关于sys_context()函数功能一览</title><link>http://www.blogjava.net/martinyuan/articles/271947.html</link><dc:creator>Martin Yuan</dc:creator><author>Martin Yuan</author><pubDate>Thu, 21 May 2009 05:49:00 GMT</pubDate><guid>http://www.blogjava.net/martinyuan/articles/271947.html</guid><wfw:comment>http://www.blogjava.net/martinyuan/comments/271947.html</wfw:comment><comments>http://www.blogjava.net/martinyuan/articles/271947.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/martinyuan/comments/commentRss/271947.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/martinyuan/services/trackbacks/271947.html</trackback:ping><description><![CDATA[
		<p>包括取本地ip，服务器端domian/db_name……，还有当前session的信息<br />后面有使用举例：<br />AUTHENTICATION_DATA<br />Data being used to authenticate the login user. For X.503 certificate authenticated sessions, this field returns the context of the certificate in HEX2 format.<br />256</p>
		<p>
				<br />Note: You can change the return value of the AUTHENTICATION_DATA attribute using the length parameter of the syntax. Values of up to 4000 are accepted. This is the only attribute of USERENV for which Oracle implements such a change.<br /> </p>
		<p>AUTHENTICATION_TYPE<br />How the user was authenticated:</p>
		<p>DATABASE: username/password authentication</p>
		<p>OS: operating system external user authentication</p>
		<p>NETWORK: network protocol or ANO authentication</p>
		<p>PROXY: OCI proxy connection authentication</p>
		<p>  30</p>
		<p>BG_JOB_ID<br />Job ID of the current session if it was established by an Oracle background process. Null if the session was not established by a background process.<br />30</p>
		<p>CLIENT_INFO<br />Returns up to 64 bytes of user session information that can be stored by an application using the DBMS_APPLICATION_INFO package.<br />64</p>
		<p>CURRENT_SCHEMA<br />Name of the default schema being used in the current schema. This value can be changed during the session with an ALTER SESSION SET CURRENT_SCHEMA statement.<br />30</p>
		<p>CURRENT_SCHEMAID<br />Identifier of the default schema being used in the current session.<br />30</p>
		<p>CURRENT_USER<br />The name of the user whose privilege the current session is under.<br />30</p>
		<p>CURRENT_USERID<br />User ID of the user whose privilege the current session is under<br />30</p>
		<p>DB_DOMAIN<br />Domain of the database as specified in the DB_DOMAIN initialization parameter.<br />256</p>
		<p>DB_NAME<br />Name of the database as specified in the DB_NAME initialization parameter<br />30</p>
		<p>ENTRYID<br />The available auditing entry identifier. You cannot use this option in distributed SQL statements. To use this keyword in USERENV, the initialization parameter AUDIT_TRAIL must be set to true. <br />30</p>
		<p>EXTERNAL_NAME<br />External name of the database user. For SSL authenticated sessions using v.503 certificates, this field returns the distinguished name (DN) stored in the user certificate.<br />256</p>
		<p>FG_JOB_ID<br />Job ID of the current session if it was established by a client foreground process. Null if the session was not established by a foreground process.<br />30</p>
		<p>HOST<br />Name of the host machine from which the client has connected.<br />54</p>
		<p>INSTANCE<br />The instance identification number of the current instance. <br />30</p>
		<p>IP_ADDRESS<br />IP address of the machine from which the client is connected.<br />30</p>
		<p>ISDBA<br />TRUE if you currently have the DBA role enabled and FALSE if you do not. <br />30</p>
		<p>LANG<br />The ISO abbreviation for the language name, a shorter form than the existing 'LANGUAGE' parameter.<br />62</p>
		<p>LANGUAGE<br />The language and territory currently used by your session, along with the database character set, in this form:</p>
		<p>language_territory.characterset<br />52</p>
		<p>NETWORK_PROTOCOL<br />Network protocol being used for communication, as specified in the 'PROTOCOL=protocol' portion of the connect string.<br />256</p>
		<p>NLS_CALENDAR<br />The current calendar of the current session.<br />62</p>
		<p>NLS_CURRENCY<br />The currency of the current session.<br />62</p>
		<p>NLS_DATE_FORMAT<br />The date format for the session.<br />62</p>
		<p>NLS_DATE_LANGUAGE<br />The language used for expressing dates.<br />62</p>
		<p>NLS_SORT<br />BINARY or the linguistic sort basis.<br />62</p>
		<p>NLS_TERRITORY<br />The territory of the current session.<br />62</p>
		<p>OS_USER<br />Operating system username of the client process that initiated the database session<br />30</p>
		<p>PROXY_USER<br />Name of the database user who opened the current session on behalf of SESSION_USER.<br />30</p>
		<p>PROXY_USERID<br />Identifier of the database user who opened the current session on behalf of SESSION_USER.<br />30</p>
		<p>SESSION_USER<br />Database user name by which the current user is authenticated. This value remains the same throughout the duration of the session.<br />30</p>
		<p>SESSION_USERID<br />Identifier of the database user name by which the current user is authenticated.<br />30</p>
		<p>SESSIONID<br />The auditing session identifier. You cannot use this option in distributed SQL statements.<br />30</p>
		<p>TERMINAL<br />The operating system identifier for the client of the current session. In distributed SQL statements, this option returns the identifier for your local session. In a distributed environment, this is supported only for remote SELECT statements, not for remote INSERT, UPDATE, or DELETE operations. (The return length of this parameter may vary by operating system.)</p>
		<p> </p>
		<p>
				<br />SQL&gt; SELECT sys_context('USERENV','IP_ADDRESS')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','IP_ADDR                                                 <br />--------------------------------------------------------------------------------<br />10.0.0.177 </p>
		<p>SQL&gt; SELECT SYS_CONTEXT ('hr_apps', 'group_no') "User Group"<br />  2     FROM DUAL;</p>
		<p>User Group                                                                     <br />--------------------------------------------------------------------------------</p>
		<p>SQL&gt; SELECT sys_context('USERENV','CLIENT_INFO')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','CLIENT_                                                 <br />--------------------------------------------------------------------------------</p>
		<p>SQL&gt; SELECT sys_context('USERENV','CURRENT_SCHEMA')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','CURRENT                                                 <br />--------------------------------------------------------------------------------<br />IDCBJ                                                                          </p>
		<p>SQL&gt; SELECT sys_context('USERENV','HOST')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','HOST')                                                  <br />--------------------------------------------------------------------------------<br />WORKGROUP\RAINY                                                                </p>
		<p>SQL&gt; SELECT sys_context('USERENV','AUTHENTICATION_DATA')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','AUTHENT                                                 <br />--------------------------------------------------------------------------------</p>
		<p>SQL&gt; SELECT sys_context('USERENV','AUTHENTICATION_TYPE')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','AUTHENT                                                 <br />--------------------------------------------------------------------------------<br />DATABASE                                                                       </p>
		<p>SQL&gt; SELECT sys_context('USERENV','DB_DOMAIN')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','DB_DOMA                                                 <br />--------------------------------------------------------------------------------<br />chenyq                                                                         </p>
		<p>SQL&gt; SELECT sys_context('USERENV','DB_NAME')  FROM DUAL;</p>
		<p>SYS_CONTEXT('USERENV','DB_NAME                                                 <br />--------------------------------------------------------------------------------<br />ora817                                                                         </p>
		<p>SQL&gt; SELECT SYS_CONTEXT('hr_context', 'org_id')  FROM DUAL;</p>
		<p>SYS_CONTEXT('HR_CONTEXT','ORG_                                                 <br />--------------------------------------------------------------------------------</p>
		<p>SQL&gt;</p>
<img src ="http://www.blogjava.net/martinyuan/aggbug/271947.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/martinyuan/" target="_blank">Martin Yuan</a> 2009-05-21 13:49 <a href="http://www.blogjava.net/martinyuan/articles/271947.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ORACLE 中ROWNUM用法总结!(精华)</title><link>http://www.blogjava.net/martinyuan/articles/271948.html</link><dc:creator>Martin Yuan</dc:creator><author>Martin Yuan</author><pubDate>Thu, 21 May 2009 05:49:00 GMT</pubDate><guid>http://www.blogjava.net/martinyuan/articles/271948.html</guid><wfw:comment>http://www.blogjava.net/martinyuan/comments/271948.html</wfw:comment><comments>http://www.blogjava.net/martinyuan/articles/271948.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/martinyuan/comments/commentRss/271948.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/martinyuan/services/trackbacks/271948.html</trackback:ping><description><![CDATA[对于 Oracle 的 rownum 问题，很多资料都说不支持&gt;,&gt;=,=,between...and，只能用以上符号(&lt;、&lt;=、!=)，并非说用&gt;,&gt;=,=,between..and 时会提示SQL语法错误，而是经常是查不出一条记录来，还会出现似乎是莫名其妙的结果来，其实您只要理解好了这个 rownum 伪列的意义就不应该感到惊奇，同样是伪列，rownum 与 rowid 可有些不一样，下面以例子说明<br /><br />假设某个表 t1(c1) 有 20 条记录<br /><br />如果用 select rownum,c1 from t1 where rownum &lt; 10, 只要是用小于号，查出来的结果很容易地与一般理解在概念上能达成一致，应该不会有任何疑问的。<br /><br />可如果用 select rownum,c1 from t1 where rownum &gt; 10 (如果写下这样的查询语句，这时候在您的头脑中应该是想得到表中后面10条记录)，你就会发现，显示出来的结果要让您失望了，也许您还会怀疑是不谁删了一些记录，然后查看记录数，仍然是 20 条啊？那问题是出在哪呢？<br /><br />先好好理解 rownum 的意义吧。因为ROWNUM是对结果集加的一个伪列，即先查到结果集之后再加上去的一个列 (强调：先要有结果集)。简单的说 rownum 是对符合条件结果的序列号。它总是从1开始排起的。所以你选出的结果不可能没有1，而有其他大于1的值。所以您没办法期望得到下面的结果集：<br /><br />11 aaaaaaaa<br />12 bbbbbbb<br />13 ccccccc<br />.................<br /><br />rownum &gt;10 没有记录，因为第一条不满足去掉的话，第二条的ROWNUM又成了1，所以永远没有满足条件的记录。或者可以这样理解：<br /><br />ROWNUM是一个序列，是oracle数据库从数据文件或缓冲区中读取数据的顺序。它取得第一条记录则rownum值为1，第二条为2，依次类推。如果你用&gt;,&gt;=,=,between...and这些条件，因为从缓冲区或数据文件中得到的第一条记录的rownum为1，则被删除，接着取下条，可是它的rownum还是1，又被删除，依次类推，便没有了数据。<br /><br />有了以上从不同方面建立起来的对 rownum 的概念，那我们可以来认识使用 rownum 的几种现像<br /><br />1. select rownum,c1 from t1 where rownum != 10 为何是返回前9条数据呢？它与 select rownum,c1 from tablename where rownum &lt; 10 返回的结果集是一样的呢？<br />因为是在查询到结果集后，显示完第 9 条记录后，之后的记录也都是 != 10,或者 &gt;=10,所以只显示前面9条记录。也可以这样理解，rownum 为9后的记录的 rownum为10，因条件为 !=10，所以去掉，其后记录补上，rownum又是10，也去掉，如果下去也就只会显示前面9条记录了<br /><br />2. 为什么 rownum &gt;1 时查不到一条记录，而 rownum &gt;0 或 rownum &gt;=1 却总显示所以的记录<br />因为 rownum 是在查询到的结果集后加上去的，它总是从1开始<br /><br />3. 为什么 between 1 and 10 或者 between 0 and 10 能查到结果，而用 between 2 and 10 却得不到结果<br />原因同上一样，因为 rownum 总是从 1 开始<br /><br />从上可以看出，任何时候想把 rownum = 1 这条记录抛弃是不对的，它在结果集中是不可或缺的，少了rownum=1 就像空中楼阁一般不能存在，所以你的 rownum 条件要包含到 1<br /><br />但如果就是想要用 rownum &gt; 10 这种条件的话话就要用嵌套语句,把 rownum 先生成，然后对他进行查询。<br />select *<br />from (selet rownum as rn，t1.* from a where ...)<br />where rn &gt;10<br /><br />一般代码中对结果集进行分页就是这么干的。<br /><br />另外：rowid 与 rownum 虽都被称为伪列，但它们的存在方式是不一样的，rowid 可以说是物理存在的，表示记录在表空间中的唯一位置ID，在DB中唯一。只要记录没被搬动过，rowid是不变的。rowid 相对于表来说又像表中的一般列，所以以 rowid 为条件就不会有 rownum那些情况发生。<br />另外还要注意：rownum不能以任何基表的名称作为前缀。<img src ="http://www.blogjava.net/martinyuan/aggbug/271948.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/martinyuan/" target="_blank">Martin Yuan</a> 2009-05-21 13:49 <a href="http://www.blogjava.net/martinyuan/articles/271948.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>