当前位置: 代码迷 >> SQL >> oracle-sql汇总
  详细解决方案

oracle-sql汇总

热度:86   发布时间:2016-05-05 13:26:42.0
oracle--sql汇总

1sql汇总

一、重复操作查询

--where条件得distinct systemdicid作为唯一标识

select *

? from dmis_zd_systemdic t

?WHERE typeid = '06012'

?? and t.systemdicid in (select min(systemdicid)

?????????????????????????? from dmis_zd_systemdic

????????????????????????? where typeid = '06012'

????????????????????????? group by name)

?order by orderno;

二、检查表是否存在

select count(tname) from tab where tname = upper('表名');

三、日期函数

--返回当前日期年度的第一天

select trunc(sysdate,'year') from dual;

--返回当前日期月份的第一天

select trunc(sysdate,'month') from dual;

--上月最后一天

select last_day(add_months(sysdate, -1)) from dual;

--给定日期后最近星期几得日期

select next_day(to_date('2009-12-01', 'yyyy-mm-dd'), '星期一') next_day

from dual;

四、同一张表中,根据一个字段更新另一个字段

update (select t.fgenerationtime as ftime, t.fgeneratedateall as str

????????? from dmis_fs_approvebook t

???????? where t.fgenerationtime is not null)

?? set str = TO_CHAR(ftime, 'yyyy-mm-dd')

?where str is null;

五、重复数据查询

select * FROM EMP E

WHERE E.ROWID > (SELECT MIN(X.ROWID)

FROM EMP X

WHERE X.EMP_NO = E.EMP_NO);

六、合并不同表的数据(merge ?into

merge into student s

using (select id, name, tel from test001) x

on (s.s_id = x.id)

when matched then

? update set s_name = x.name

when not matched then

? insert (s_id, s_name, s_age) values (x.id, x.name, x.tel);

commit;

七、查询执行sqlv$sql

select t.module, t.first_load_time, t.sql_text

? from v$sql t

?order by first_load_time desc;

2、数据库精度修改处理

-- Create table

/*drop table temp_data;*/

create table temp_data

(

? FID????? VARCHAR2(40) not null,

? USEHOURS NUMBER(10) default 0,

? FVOLTAGE NUMBER(10) default 0,

? INVOLTAGE NUMBER(10) default 0

)

;

alter table TEMP_DATA

? add constraint tempfid primary key (FID);

?

insert into temp_data

? select a.fid, a.usehours, a.fvoltage, a.involtage

??? from dmis_fs_factorymonthdetail a;

?

update dmis_fs_factorymonthdetail t

?? set t.usehours = '', t.fvoltage = '', t.involtage = '';

?

alter table DMIS_FS_FACTORYMONTHDETAIL modify USEHOURS NUMBER(10,1);

alter table DMIS_FS_FACTORYMONTHDETAIL modify FVOLTAGE NUMBER(10,1);

alter table DMIS_FS_FACTORYMONTHDETAIL modify INVOLTAGE NUMBER(10,1);

?

update (select a.usehours? as tusehours,

?????????????? b.usehours? as fusehours,

?????????????? a.fvoltage? as tfvoltage,

?????????????? b.fvoltage? as ffvoltage,

?????????????? a.involtage as tinvoltage,

?????????????? b.involtage as finvoltage,

?????????????? a.fid?????? as ffid,

?????????????? b.fid?????? as tfid

????????? from dmis_fs_factorymonthdetail a, temp_data b

???????? where a.fid = b.fid) tt

?? set tt.tusehours? = tt.fusehours,

?????? tt.tfvoltage? = tt.ffvoltage,

?????? tt.tinvoltage = tt.finvoltage

?where ffid = tfid;

??

drop table temp_data;

commit;

?

3、恢复drop掉的存储过程

sys用户登陆,执行如下的查询:

SQL> select text from dba_source as of timestamp to_timestamp('2009-03-06 09:45:00', 'YYYY-MM-DD HH24:MI:SS') where owner='IPRA' and name= 'P_IPACCHECK_NC' order by line;

4删除某个用户下的对象

--删除某个用户下的对象??

set heading off;??

set feedback off;??

spool c:\dropobj.sql;??

? prompt --Drop constraint??

?select 'alter table '||table_name||' drop constraint '||constraint_name||' ;' from user_constraints where constraint_type='R';??

?prompt --Drop tables??

?select 'drop table '||table_name ||';' from user_tables;???

???

?prompt --Drop view??

?select 'drop view ' ||view_name||';' from user_views;??

???

?prompt --Drop sequence??

?select

1 楼 wujiazhao88 2010-04-06  
其实我建议在sql里面不要太多的业务逻辑。逻辑交给APP去处理吧
2 楼 Rexwong 2010-04-06  
wujiazhao88 写道
其实我建议在sql里面不要太多的业务逻辑。逻辑交给APP去处理吧

恩,的确是这个道理。但是有时报表需要写sql,还有就是项目上线时对于数据库的更改,弄成脚本,还是有他的好处的
3 楼 helyho 2010-04-07  
应用的维护是维护中成本的大头.所以我还是认为sql放在库中较好,但前提是管理好版本.
4 楼 刃之舞 2010-04-07  
wujiazhao88 写道
其实我建议在sql里面不要太多的业务逻辑。逻辑交给APP去处理吧

平衡APP服务器和数据库的双方压力,有些逻辑在数据库中处理更轻便
5 楼 Rexwong 2010-04-07  
helyho 写道
应用的维护是维护中成本的大头.所以我还是认为sql放在库中较好,但前提是管理好版本.

是,以前我们的项目有近一百个存储过程,并且分布了十多个节点。如果同步到这样的生产环境,如何管理版本,着实是一个问题,不知道兄弟有什么好方法。
6 楼 05150214 2010-04-07  
想不明白把业务逻辑放到app中处理?  明明应该在数据库里解决的
7 楼 andyu2008 2010-04-07  
楼主写这些SQL的时候,考虑过性能问题没?
8 楼 yn5411 2010-04-07  
我的insert,update,delete是app做,其它的特别是报表数据用存储过程来完成,充分利用数据库。
9 楼 Rexwong 2010-04-08  
andyu2008 写道
楼主写这些SQL的时候,考虑过性能问题没?

以上sql没有考虑性能,因为大多数都是支持测试部署人员进行数据库更新用的。均是他们在数据库直接操作。
10 楼 ironsabre 2010-04-13  
Rexwong 写道
helyho 写道
应用的维护是维护中成本的大头.所以我还是认为sql放在库中较好,但前提是管理好版本.

是,以前我们的项目有近一百个存储过程,并且分布了十多个节点。如果同步到这样的生产环境,如何管理版本,着实是一个问题,不知道兄弟有什么好方法。


跟管理java版本有区别吗?我们有更多的存储过程,从来没有听过有版本管理上的问题。
11 楼 sokoo108 2010-04-15  
刃之舞 写道
wujiazhao88 写道
其实我建议在sql里面不要太多的业务逻辑。逻辑交给APP去处理吧

平衡APP服务器和数据库的双方压力,有些逻辑在数据库中处理更轻便

的确,有些业务算法对数据的依赖很大,在数据库中执行会提高不少效率。
12 楼 ssqmeng 2010-04-15  
收藏了:)
  相关解决方案