select sum(njts)-sum(ysyts) from njsyqk where ygdh=‘888882‘ and ((yxbz is null) or (yxbz=‘1‘))
select qjd_lsh,qjd_jqlx,qjd_sy,qjd_ygjs,jbxx_sszz from qjd q,ygjbxx y where q.qjd_ygbh=y.jbxx_bh and qjd_lsh=(select max(qjd_lsh) from qjd where qjd_ygbh=‘888882‘)
String sql = "select KS_LS_SFZH ,KS_LS_PASSWORD,KS_LS_NAME,KS_LS_SJ, KS_LS_EMAIL,decode(KS_LS_SEX,1,‘男‘,2,‘女‘) KS_LS_SEX,ks_ls_zcsj from ks_lsygb where ks_lsygb.ks_ls_sfzh in(select KS_LS_SFZH from ks_lsygb minus select ks_ksryb.ygdh from ks_ksryb where 1=1 and length(ks_ksryb.ygdh)>6 and ks_ksryb.ksbh=‘"
+ ksbh + "‘) order by ks_ls_zcsj desc";
select KS_LS_SFZH ,KS_LS_PASSWORD,KS_LS_NAME,KS_LS_SJ, KS_LS_EMAIL,decode(KS_LS_SEX,1,‘男‘,2,‘女‘) KS_LS_SEX,ks_ls_zcsj
from ks_lsygb where ks_lsygb.ks_ls_sfzh in
(select KS_LS_SFZH from ks_lsygb
minus select ks_ksryb.ygdh from ks_ksryb
where 1=1 and length(ks_ksryb.ygdh)>6 and ks_ksryb.ksbh=‘651‘) order by ks_ls_zcsj desc
组合条件作为OR的一个选项:
and ((yg.jbxx_name=‘"+cjtjPO.getXm()+"‘ and sjp.sjbp_ygdh=yg.jbxx_bh and sjp.sjbp_ygdh= ry.ygdh) or (ls.ks_ls_name=‘"+cjtjPO.getXm()+"‘ and sjp.sjbp_ygdh=ls.ks_ls_sfzh and sjp.sjbp_ygdh= ry.ygdh))
select decode(length(Sjbp_Ygdh),6,(select jbxx_name from ygjbxx where jbxx_bh=Sjbp_Ygdh),(select ks_ls_name from ks_lsygb where ks_ls_sfzh=Sjbp_Ygdh)) as name ,
decode(length(Sjbp_Ygdh),6,(select jbxx_bh from ygjbxx where jbxx_bh=Sjbp_Ygdh),(select ks_ls_sfzh from ks_lsygb where ks_ls_sfzh=Sjbp_Ygdh)) as ygdh ,
decode(length(Sjbp_Ygdh),6,(select zzjg_mc from zzjg where zzjg_bh =(select jbxx_sszz from ygjbxx where jbxx_bh = sjbp_ygdh)),(select zzjg_mc from zzjg where zzjg_bh =(select jbxx_sszz from ygjbxx where jbxx_sfzh = sjbp_ygdh))) as sszz ,
sjbp_df , sjbp_kskssj , sjbp_dths from ks_sjbp where ((sjbp_jjsj is null) or (sjbp_pjbz is null)) and sjbp_ksbh =‘1265‘
zzjg_cc in (‘1‘,‘2‘)
and substr(jbxx_sszz,1,8) = zzjg_bh
//循环获得List中的某个PO
for (int i = 0; i < bmrztjcxPOList1.size(); i++) { // 应提交人次
BmRztjCxPO bmrztjcxPO1 = bmrztjcxPOList1.get(i);}
if (bmrztjcxPO1.getBmdh().trim().equals(
bmrztjcxPO2.getBmdh().trim())) {count2 = Integer.parseInt(bmrztjcxPO2.getAstjrcs());
break;
}
bmrztjcxPO1.setAstjl(String.format("%.2f", count2 * 100.00/ count1)+ "%"); // 按时提交率
if (!bmrztjcxPOList.isEmpty()) { // 有查询记录才计算合计值
rs.getDate("rztjb_gzrq").toString().equals(rs2.getDate("rzp_gzrq").toString())
SELECT zzjg_bh,zzjg_cc,
CASE WHEN zzjg_cc=3 THEN ‘|-‘||(select zzjg_mc from zzjg a where a.zzjg_bh=z.zzjg_bh)
WHEN zzjg_cc=4 THEN ‘|--‘||(select zzjg_mc from zzjg a where a.zzjg_bh=z.zzjg_bh)
ELSE zzjg_mc END case,‘‘,‘true‘
FROM zzjg z,ygjbxx where
((substr(zzjg_bh,1,8)=substr(jbxx_sszz,1,8)) or (jbxx_sszz like ‘000302%‘ and zzjg_bh = ‘000302‘))
and jbxx_bh= ‘888882‘ and zzjg_kybz=‘1‘ order by zzjg_bh
select * from zzjg where length(zzjg_bh)=6 order by zzjg_bh asc
SELECT id,id,mc,‘‘,‘true‘ from kp_jzqk order by 1[c1]
select Xmbh,lxsj,(‘<‘||trim(Xmbh)||‘>‘||Xmmc)[c2] ,‘‘,‘true‘
from kp_xmzd where Xmjl=? order by lxsj desc
//插入字段类型为date的数据列
pstmt.setDate(4, "".equals(xmzd.getLxsjStr()) ? null : java.sql.Date.valueOf(xmzd.getLxsjStr()));
to_char(qjd_lsh)
update lcyj set spyj = ?,yy = ?,spsj = sysdate where qjdh = ? and ygbh = ? and sid = ?");
pstmt = conn.prepareStatement("insert into gzjl_rzp(rzp_ygdh,rzp_bmdh,rzp_gzrq,rzp_tjsj) values (?,?,?,to_timestamp(?,‘yyyy-mm-dd hh24:mi:ss.ff‘))");
for(String day : nday){
pstmt.setString(1, ygdh);
pstmt.setString(2, sszz);
pstmt.setDate(3, java.sql.Date.valueOf(day));
pstmt.setString(4, (day+" 23:59:59"));
pstmt.addBatch();
}
select gzjl_lxb.gzlx_mc,sum(gzjl_rzd.rzd_gzl) as gzl from gzjl_rzd inner join gzjl_lxb on gzjl_rzd.rzd_gzlx = gzjl_lxb.gzlx_bh inner join gzjl_rzp on gzjl_rzd.rzd_rzxh = gzjl_rzp.rzp_rzxh where gzjl_rzp.rzp_gzrq >= to_date(‘"+ksrq+"‘,‘yyyy-mm-dd‘) and gzjl_rzp.rzp_gzrq <= to_date(‘"+jsrq+"‘,‘yyyy-mm-dd‘) and gzjl_rzd.rzd_xmbh = ‘"+xmbh+"‘ group by gzjl_lxb.gzlx_mc order by gzl desc
// 结束时间不为空时
if (!"".equals(hkxxglQO.getJssj()) && null != hkxxglQO.getJssj()) {
sql.append(" and ls.jhk_cjsj <=to_date(‘" + hkxxglQO.getJssj() + " 23:59:59‘,‘yyyy-mm-dd hh24:mi:ss‘) ");
year(to_date(‘2013-6-9’,‘yyyy-mm-dd‘)//返回2013,同样可以将year改为month、day
了解当前各个用户的名字及他们所进行的操作时,要用下面命令才能得到详细的结果
select sid,serial#,username,
DECODE(command,
0,’None’,
2,’Insert’,
3,’Select’,
6,’Update’,
7,’Delete’,
8,’Drop’,
‘Other’) cmmand
from v$session where username is not null;
- 在使用Oracle的 to_date 函数来做日期转换时,可能会直觉地采用“yyyy-MM-dd HH:mm:ss”的格式作为格式进行转换,但是在Oracle中会引起错误:“ORA 01810 格式代码出现两次”。如:select to_date (‘2005-01-01 13:14:20‘,‘yyyy-MM-dd HH24:mm:ss‘) from dual;原因是SQL中不区分大小写,MM和mm被认为是相同的格式代码,所以Oracle的SQL采用了mi代替分钟。select to_date (‘2005-01-01 13:14:20‘,‘yyyy-MM-dd HH24:mi:ss‘) from dual;
2.另要以24小时的形式显示出来要用HH24
select to_char(sysdate,‘yyyy-MM-dd HH24:mi:ss‘) from dual;//mi是分钟/*mi代表分钟2013-06-14 14:36:13*/
select to_char(sysdate,‘yyyy-MM-dd HH24:mm:ss‘) from dual;//mm会显示月份 /*mm代表月份2013-06-14 14:06:14*/
TO_DATE 格式(以时间:2007-11-02 13:45:25为例)
Year:
yy two digits 两位年 显示值:07
yyy three digits 三位年 显示值:007
yyyy four digits 四位年 显示值:2007
Month:
mm number 两位月 显示值:11
mon abbreviated 字符集表示 显示值:11月,若是英文版,显示nov
month spelled out 字符集表示 显示值:11月,若是英文版,显示november
Day:
dd number 当月第几天 显示值:02
ddd number 当年第几天 显示值:02
dy abbreviated 当周第几天简写 显示值:星期五,若是英文版,显示fri
day spelled out 当周第几天全写 显示值:星期五,若是英文版,显示friday
ddspth spelled out, ordinal twelfth
Hour:
hh two digits 12小时进制 显示值:01
hh24 two digits 24小时进制 显示值:13
Minute:
mi two digits 60进制 显示值:45
Second:
ss two digits 60进制 显示值:25
其它
Q digit 季度 显示值:4
WW digit 当年第几周 显示值:44
W digit 当月第几周 显示值:1
24小时格式下时间范围为: 0:00:00 - 23:59:59....
12小时格式下时间范围为: 1:00:00 - 12:59:59 ....
select floor(sysdate-to_date(‘20120614‘,‘yyyymmdd‘)) from dual/*两个日期之间的天数*/
select sysdate 现在日期,add_months(sysdate,1) 一个月后的今天 from dual
select to_date(sysdate,‘yyyy-mm-dd‘), add_months(to_date(sysdate,‘yyyy-mm-dd‘),1) from dual
select add_months(to_date(‘2000-01-31‘,‘YYYY-MM-DD‘),1) from dual/*2000-2-29*/
select sysdate from dual
select to_char(6666,‘$9,999.99‘) from dual/* $6,666.00*/
select to_date(‘2013-11-01 19:25:34‘,‘YYYY-MM-DD HH24:MI:SS‘) from dual
select to_date (‘2005-01-01 13:14:2‘,‘yyyy-MM-dd HH24:mi:ss‘) from dual/*2005-1-1 13:14:02*/
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual/*mi代表分钟2013-06-14 14:36:13*/
select to_char(sysdate,‘yyyy-mm-dd hh24:mm:ss‘) from dual/*mm代表月份2013-06-14 14:06:14*/
select to_char(sysdate,‘yy-mm-dd‘) from dual/*13-06-14*/
select to_char(sysdate,‘yyyy-mon-dd-day‘) from dual/*2013-6月 -14-星期五*/
select to_char(sysdate,‘yyyy-month-dd-day‘) from dual/*2013-6月 -14-星期五*/
select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘) from dual/*2013-06-14 17:01:09*/
select to_char(sysdate,‘yyyy‘) from dual/*2013*/
select to_char(to_date(‘2013-06-14‘,‘yyyy-mm-dd‘),‘day‘) from dual/*星期五*/
select floor(sysdate-to_date(‘20120614‘,‘yyyymmdd‘)) from dual/*两个日期之间的天数*/
select floor(sysdate-to_date(‘2012-06-14‘,‘yyyy-mm-dd‘)) from dual/*两个日期之间的天数*/
//主键冲突
java.sql.SQLException: 1
//外键冲突
java.sql.SQLException: 2292
Select A.* from A,B where A.city = b.city
values(SEQ_XS_THD.nextval,//实现数据【主键】自增
//
游标为应用程序提供了一种对具有多行数据的查询结果集中每行数据进行单独处理的方法,是设计交互式应用程序的常用编程接口
declare
v_age stu.age%type;
v_total_age stu.age%type;
courso cur_var is select age from stu;--创建游标begin
v_total_age :=0;
open cur_var;--打开游标
fetche cur_var into v_age;
while cur_var%found loop--遍历游标
v_total_age :=v_total_age+v_age;
fetch cur_var into v_age;
end loop;
close cur_var;end;
通过上面的例子,下面简单介绍游标的属性:
(1)%FOUND – SQL 语句影响了一行或多行时为 TRUE,即游标找到数据就返回TRUE,反之则返回FALSE
(2)%NOTFOUND – SQL 语句没有影响任何行时为TRUE,即游标没有找到数据就返回TRUE,反之则返回FALSE
(3)%ROWCOUNT – SQL 语句影响的行数,即返回游标所找到的数据行数(返回一个整数)
(4)%ISOPEN - 游标是否打开,即游标打开为TRUE,反之则为FALSE
使用显式游标更新行:
允许使用游标删除或更新活动集中的行
声明游标时必须使用 SELECT … FOR UPDATE语句
CURSOR <cursor_name> IS
SELECT statement FOR UPDATE;
UPDATE <table_name>
SET <set_clause>
WHERE CURRENT OF <cursor_name>例子:
DECLARE
new_price NUMBER;
CURSOR cur_toy IS
SELECT toyprice FROM toys WHERE toyprice<100
FOR UPDATE OF toyprice;
BEGIN
OPEN cur_toy;
LOOP
FETCH cur_toy INTO new_price;
EXIT WHEN cur_toy%NOTFOUND;
UPDATE toys
SET toyprice = 1.1*new_price
WHERE CURRENT OF cur_toy;
END LOOP;
CLOSE cur_toy;
COMMIT;
END;
//函数
/*********************创建函数********************/
/*day函数*/
create or replace function day(date1 date)
return integer is
begin
return to_number(to_char(date1,‘dd‘));
end day;
/*月份*/
create or replace function month(date1 date)
return integer is
begin
return to_number(to_char(date1,‘mm‘));
end month;
/*年*/
create or replace function year(date1 date)
return integer is
begin
return to_number(to_char(date1,‘yyyy‘));
end year;
/**/
create or replace function weekday(date1 date)
return integer is
begin
return to_number(to_char(date1,‘D‘))-1;
end weekday;
/*Oracle自定义函数*/
/*语法如下*/
create or replace function function_name(argu1 [model] datatype1,agru2 [mode2] datatype2)
return datatype is
begin
end;
/*执行*/
var v1 varchar2(100)
exec :v1:=function_name
/*=====不带任何参数====*/
create or replace function get_user
return varchar2 is Result varchar2(50);
begin
select username into Result from user_users;
return (Result);
end get_user;
/*======带in参数的=====*/
create or replace function get_sal(empname in varchar2)
return number is
Result number;
begin
select sal into Result from emp where ename=empname;
return (Result);
end get_sal;
/*执行:*/
SQL>var sal number
SQL>exec :sal:=get_sal(‘scott‘);
/*带out参数的函数*/
create or replace function get_info(e_name varchar2,job out varchar2)
return number is
Result number;
begin
select sal,job into Result,job from emp where ename=e_name;
return (Result);
end get_info;
/*执行*/
SQL>var job varcha2(20);
SQL>var dname varchar2(20)
SQL>exec :dname:=get_info(‘scott‘,:job)
/*=====带in out 参数的函数=====*/
create or replace function result(num1 number,num2 in out number)
return number is
v_result number(6);
v_remainder number;
begin
v_reuslt:=num1/num2;
v_remainder:=mod(num1,num2);
num2:=v_remainder;
return (v_result);
Exception when zero_divide then
raise_application_error(-20000,‘不能除0‘);
end result;
/*执行*/
var result1 number;
var result2 number;
exec :result2:=30
exec :result1:=result(100,:result2) eg:
/*带有IN参数的函*/
create or replace function get_empname(v_id in number)
return varchar2 is v_name varchar2(50);
begin
select name into v_name from employee where id=v_id;
reutrn v_name;
exception
when no_data_found then
raise_application_error(-20001,‘你输入的ID无效!‘);
end get_empname;
/*1、一个最简单的自定义函数Fun_test1的定义*/
create or replace function Fun_test1(p_1 number)/*number型输入参数p_1*/
return number is /*返回值也是number型*/
begin
if p_1>0 then return 1;
elsif p_1=0 then return 0;
else return -1;
end if;
end;
/*2、Fun_test1 自定义函数的调用的存储过程Pro_Fun_test1_1示例:一个输入参数,一个输出参数*/
create or replace procedure Pro_Fun_test1_1(p1_in number,p2_out out number)
as begin p2_out:=Fun_test1(p1_in);
end Pro_Fun_test1_1;
/*3、Fun_test1自定义函数的调用的存储过程Pro_Fun_test1_2示例*/
create or replace procedure Pro_Fun_test1_2(p1_in in number,p2_out out number)
as t_1 number;
begin
select Fun_test1(p1_in)+100 into p2_out from bill_org where org_ID=1;
end Pro_Fun_test1_2;
//当查询同一表的同一列查询两次时,按查询结果的第一列排序则可以按一下方法,否则会报列名不确定
SELECT zqmx_bh,zqmx_bh,zqmx_mc FROM xmgl_smzqmx order by 1
//一次向表中插入多条记录
insert into fy_jhk_lcyj(Jhk_jhkh,Jhk_lcsx,Jhk_spjs,Jhk_spr,Jhk_splx,Jhk_spyj,Jhk_spsj) select ‘110‘,‘0‘,‘05‘,‘888882‘,‘0‘,‘‘,sysdate from dual union all select ‘110‘,‘1‘,‘02‘,‘‘,‘‘,‘‘,null from dual
//返回年 如‘2013’
select year(sysdate) from dual
//返回年组:如今年为2013则以下SQL返回:2010、2011、2012、2013、2014
select year,year,year from
(
select to_number(to_char(sysdate,‘yyyy‘)) year from dual
union
select to_number(to_char(sysdate,‘yyyy‘))-rownum year from dual connect by level<=3
union
select to_number(to_char(sysdate,‘yyyy‘))+rownum year from dual connect by level<=1
)
select jcfl_bh,jcfl_mc,(‘<‘||trim(jcfl_bh)||‘>‘||jcfl_mc) from xm_jcflzd
//获取查询结果中指定行的数据
select ztbg_id from (select rownum r,ztbg_id from xm_ztbg where ztbg_xmid=‘3840‘ order by ztbg_id desc) where r=2 //获取第二行的数据
select substr(sszzbh,2,4) from xs_ryjg//从第二位开始截取四位,2并非索引
substr(sszzbh,0,4)和substr(sszzbh,1,4)是相同的都是从第一位开始截取4位
删除表空间及其中的内容:
drop tablespace xxx including contents and datafiles;
查询空表,避免因无数据造成空表无法导出的问题:(将查询结果导出到Excel 然后赋值出来运行:)
select ‘alter table ‘||table_name||‘ allocate extent;‘ from user_tables where num_rows=0
导入dmp命令:
imp xxfbgswl/[email protected] file=e:\gswl20130424.dmp fromuser=gswl touser=xxfbgswl log=e:xxfblog.log rows=y
导出dmp命令:【非本地数据库在@后添加ip/】
exp gswleq/[email protected] file=D:\gswl0619.dmp log=gswl.log owner=(gswleq) rows=y
定时备份:
e:
cd app\cvicdns\product\11.2.0\dbhome_1\BIN
EXP dns/[email protected] file=e:\dnsbackup\%date:~0,10%.dmp log=e:\dnsbackup\%date:~0,10%.log compress=n buffer=8092 consistent=y direct=n constraints=y feedback=10000 grants=y record=y indexes=y triggers=y rows=y
[c1]1代表查询的列次(第N列)
[c2]作为一个整体的查询结果列,并注意“||”的使用,和‘<’,‘>’,好牛!