38. 数据表中的字段最大数: 表或视图中的最大列数为 1000
39. oracle中的裸设备:
裸设备就是绕过文件系统直接访问的储存空间
40. 在Oracle服务器上通过SQLPLUS查看本机IP地址 ? select sys_context('userenv','ip_address') from dual; 如果是登陆本机数据库,只能返回127.0.0.1
41. 在ORACLE中取毫秒?
9i之前不支持,9i开始有timestamp.
9i可以用select systimestamp from dual;
42. 将N秒转换为时分秒格式?
set serverout on declare N number := 1000000; ret varchar2(100); begin
ret := trunc(n/3600) || '小时' || to_char(to_date(mod(n,3600),'sssss'),'fmmi\分 \秒\
dbms_output.put_line(ret); end;
43、在某个用户下找所有的索引 Select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name
from user_ind_columns, user_indexes
where user_ind_columns.index_name = user_indexes.index_name and user_ind_columns.table_name = user_indexes.table_name order by user_indexes.table_type, user_indexes.table_name, user_indexes.index_name, column_position;
44. not in的替代。
一般not in的效率比较低。特别是数据量大的时候,几乎不能执行。 用下面几种方式可以替换写法
比如要查询在fee_rev_info表中已经销户的用户(不在cm_user中的)(不过下面的例子不是很好,因为bill_id是cm_user的唯一索引)
select * from fee_rev_info where bill_id not in (select bill_id from cm_user) <1> 用not exists
select * from fee_rev_info a where not exists (select 'p' from cm_user b where b.bill_id = a.bill_id) <2> 用外连接(+)
select a.* from fee_rev_info a,cm_user b where a.bill_id = b.bill_id (+) and b.bill_id is null <3> 用hash_aj
select /*+HASH_AJ*/* from fee_rev_info where bill_id not in (select bill_id from cm_user)
45.怎么样查询特殊字符,如通配符%与_
假如数据库中有表 STATIONTYPE,STATION_571 STATION_572 ... select * from tab where tname like 'STATION_%'
会显示 STATIONTYPE,STATION_571 ... 可以用下面的语句
select * from tab where tname like 'STATION\\_%' escape'\\'
46.如果存在就更新,不存在就插入可以用一个语句实现吗 9i已经支持了,是Merge,但是只支持select子查询,
如果是单条数据记录,可以写作select .... from dual的子查询。 语法为:
MERGE INTO table USING data_source ON (condition)
WHEN MATCHED THEN update_clause
WHEN NOT MATCHED THEN insert_clause; 如
MERGE INTO cm_user_credit
USING (select * from dual) ON (user_id =1302514690 ) when MATCHED then update set credit_value = 1000 when NOT MATCHED then (user_id,acc_id,bill_id,plan_id,region_code,credit_value)
values(1302514690,1305032158,'13857141218',10070247,'571',1000);
47.怎么实现一条记录根据条件多表插入
9i以上可以通过Insert all语句完成,仅仅是一个语句,如:
INSERT ALL WHEN (id=1) THEN INTO table_1 (id, name) values(id,name) WHEN (id=2) THEN INTO table_2 (id, name) values(id,name) ELSE INTO table_other (id, name) values(id, name)
SELECT id,name FROM a;
如果没有条件的话,则完成每个表的插入,如
INSERT ALL INTO table_1 (id, name) values(id,name) INTO table_2 (id, name) values(id,name)
INTO table_other (id, name) values(id, name)
insert SELECT id,name FROM a;
48.如何实现行列转换
<1>、固定列数的行列转换 如
student subject grade
--------------------------- student1 语文 80 student1 数学 70 student1 英语 60 student2 语文 90 student2 数学 80 student2 英语 100 ...
转换为
语文 数学 英语 student1 80 70 60 student2 90 80 100 ...
语句如下:
select student,sum(decode(subject,'语文', grade,null)) \语文\sum(decode(subject,'数学', grade,null)) \数学\sum(decode(subject,'英语', grade,null)) \英语\from table
group by student
<2>、不定列行列转换 如 c1 c2
-------------- 1 我 1 是 1 谁 2 知 2 道 3 不 ... 转换为 1 我是谁 2 知道 3 不
这一类型的转换必须借助于PL/SQL来完成,这里给一个例子 CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)
RETURN VARCHAR2 IS
Col_c2 VARCHAR2(4000); BEGIN
FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP Col_c2 := Col_c2||cur.c2; END LOOP;
Col_c2 := rtrim(Col_c2,1); RETURN Col_c2; END; /
SQL> select distinct c1 ,get_c2(c1) cc2 from table;即可
--例子:
create table okcai_1 (
user_id varchar2(10),
user_number varchar2(10), user_num number(8) )
user_id user_number user_num --------------------- 1 123 2 1 456 5 1 789 6 2 11 2 2 22 3 2 33 4 2 44 5 2 55 6 2 66 7 2 77 8 3 1234 1 3 5678 2
方式一:
create or replace function get_col( p_userId number, p_col number ) return varchar as
v_tmp varchar2(255); begin

