假如数据库中有表 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 insert (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) 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_c2cur.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);
|