Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31748
  • 博文数量: 8
  • 博客积分: 272
  • 博客等级: 二等列兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 17:18
文章分类

全部博文(8)

文章存档

2012年(8)

分类: Oracle

2012-12-12 14:30:44

1、查询数据库相关对象在哪里有引用:

select * from dba_dependencies a where a.referenced_name=upper('查询对象');

查询对象可以是表,过程等。比如说想找某个存储过程,但是不知道名字,可以通过涉及的表来查找

2、执行存储过程的方法
1)begin XXX; end;
2)命令行下 exec XXX;

3、查看oracle job信息
select * from dba_job;

4、查看表情况
select * from dba_tables where table_name='PRODUCT';

5、存储过程中的sql%found sql%notfound sql%rowcount
当执行完一条dml语句后,可以用上述所提的来判断,例:
update product p set  p.isshop=1 where p.id=n_shopids(i) and (p.isshop is null or p.isshop = 0);
 if (sql%found) then
...


6、with as (select *...) temp select * ....

with as临时表,有时候用来查多表里的数据蛮好用的


7、merge into ... using... on...when matched then ...when not matched then...结构

该结构可以通过来对其中一个关联表的条件判断,来对另外一个表进行更新和插入操作。

ex:

merge into student t1

using teacher t2

on (t1.teacherid=t2.id)

when matched then

update set t1.score = t1.score+5,...(可以更新多个字段,这里也可以写插入语句)

when not matched then 

insert values(...);(更新语句或者插入语句都可以)

该结构的效率比普通的更新语句快多了。


8、发现一个对于排序比较好的用法
适用情况:当提取某条件下的一定量的数据时,可能满足这个条件的数据不够要求的数目,就必须用其它的替补,但是这个排序该怎么个排法呢,可用用下面的来:
select b.* from (select rownum rm,a.* from (
         select id,question,question_time as questionTime from auto_question
         where brand_id=3 order by decode(series_id, 21, 1, 2),question_time desc)a
         where rownum<=10)b where rm>=1
红色字体,意思是series_id=21,那这条记录的排序字段是1,其余的都为2,order by 默认排序为升序,因此记录为21的肯定是在前面了。根据自身情况可以修改decode的项。


9、start with ... connect by prior...结构
这是用来查询层级数据的,就是说在一张表里存在父级数据和子级数据,通过这种查询方式可以将某个父节点下的所有子节点(包括子节点的子节点)的数据都查询出来。
例:
表 T 结构:
id  name  fatherid
1   aa       -1
2   bb       -1
3    cc       1
4    dd       1
5    ee       3
6    ff         3
7    hh        2

select id from T start with id=1 connect by prior id=fatherid
查询出来的数据:
id  name  fatherid
1   aa       -1
3    cc       1
4    dd       1
5    ee       3
6    ff         3

就是将id=1的数据以及它的节点数据全部查找出来,有多少层就查询查寻出来多少层。这是一个递归的操作,因此要谨慎使用
如果不加prior关键字,只会查询出
id  name  fatherid
1   aa       -1









日后将会把日常工作中用到的比较好用的sql语句记录下来。。。。
阅读(824) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~