本文目录
- oracle sql处理库存先进先出逻辑(急)
- oracle 触发器执行过程中出错
- 为什么oracle 不能用 =NUll 而要用 IS NULL
- 如何在ORACLE中使用ISNULL来实现NVL
- oracle中无法修改为null怎么办
oracle sql处理库存先进先出逻辑(急)
create table 表1(货号 varchar(20),批次 int ,数量 int)create table 表2(货号 varchar(20) ,数量 int)/*------------------------------*/insert into 表1select ’001’, 1, 100 union all select ’001’, 2, 200 union allselect ’001’, 3, 300 /*------------------------------*/insert into 表2select ’001’, 400/*------------------------------*/select * from 表1select * from 表2/*------------------------------*/select t1.货号,t1.批次, case when ((select isnull(sum(数量),0) from 表1 t3 where t3.货号=t1.货号 and t3.批次《t1.批次)-isnull(t2.new_数量,0))《0 then case when ((select sum(数量) from 表1 t4 where t4.货号=t1.货号 and t4.批次《=t1.批次)-isnull(t2.new_数量,0))《0 then 0 else ((select sum(数量) from 表1 t4 where t4.货号=t1.货号 and t4.批次《=t1.批次)-isnull(t2.new_数量,0)) end else t1.数量 end as 批次剩余库存数from 表1 t1 left join (select 货号,sum(数量) as new_数量 from 表2 group by 货号) t2 on t1.货号=t2.货号/*------------------------------*/PS:上面将null转换为0的函数为isnull,是SQL SERVER下的函数,你只要根据你的数据转换为对应函数就可以了,如oracle是nvl,mysql是ifnull等等.
oracle 触发器执行过程中出错
你这些表s_use_info,customer_info在system用户下? system是默认dba权限的用户 如果不在 强烈不建议把触发器建到system下 你需要授权 还得把表改成用户.表名 建立到用户下 after 改成 before 可以了CREATE OR REPLACE TRIGGER "S_USE_TRIGGER" before insert on s_use_info for each rowbeginupdate customer_info set log_op_date=sysdate where server_id=:new.server_id;end s_use_trigger;ALTER TRIGGER "S_USE_TRIGGER" ENABLE
为什么oracle 不能用 =NUll 而要用 IS NULL
NULL表示什么也没有,也就是为空的意思,而=null,则表示里面的内容为null,从表的显示上看,里面的内容为null,实际上数据库里并没有存储任何内容。为此,特别规定当某一字段中没有值时,就用isnull表示,反之,就用isnotnull表示。
如何在ORACLE中使用ISNULL来实现NVL
oracle的nvl函数用法如下:nvl(id,0),即当字段id为空时,默认给该字段赋值0,如果用isnull来实现的话,可以考虑用case when结构,即case idwhen null then 0
oracle中无法修改为null怎么办
打开PL/SQL,写如下代码declarevisnull varchar2(4);beginselect nullable into visnull from user_tab_columnswhere table_name = upper(‘tblStockInspect’)and column_name = upper(‘FDepartID’);if visnull = ‘N’ thenalter table tblStockInspect modify FDepartID int null;end if; end;
运行,又出现错误提示如下
ORA-06550: 第 8 行, 第 7 列:
PLS-00103: 出现符号 “ALTER”在需要下列之一时:( begin case declare exitfor goto if loop mod null pragma raise return select updatewhile with 《an identifier》《a double-quoted delimited-identifier》 《a bind variable》 《《continue close current delete fetch lock insert open rollbacksavepoint set sql execute commit forall merge pipe purge仔细一看,原来alter不允许在PL/SQL下直接运行,只好更改如下declarevisnull varchar2(4);beginselect nullable into visnull from user_tab_columnswhere table_name = upper(‘tblStockInspect’)and column_name = upper(‘FDepartID’);if visnull = ‘N’ thenexecute immediate ‘alter table tblStockInspect modify FDepartID int null‘;end if; end; 运行通过
oracle中null值的问题
先建一个用于测试的临时表:T1.表的内容如下:
with t1 as(select 1 num1 from dual union select null num1 from dual union select 2 num1 from dual) select * from t1;
如果我想找出num1不等于1的记录。该怎么去写sql呢?我尝试这样去写:select * from t1 where num1《》1;会得出什么结果呢?看下图:
再一次的,我把sql这样写:select * from t1 where num1《》1 or num1 is null;再来看下结果:
总结以上结果:NULL是不可以用来做比较的,无论什么值跟NULL作比较都会返回一个FALSE值。所以当记录中有NULL值的话且要处理的话要用is null来处理。