plsql问题 这句话具体什么意思

instr(nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id)),decode(t.staff_id,null,'-1','','-1',t.staff_id))>0

首先说一下decode,这是一个oracle独有的函数,功能类似于case when
举例说明:decode(a, b, c, d) 如果a = b 则返回c,否则返回d

然后是nvl,nvl(a,0)如果a的值为空,返回0

最后是instr,instr函数返回要截取的字符串在源字符串中的位置。只检索一次,就是说从字符的开始到字符的结尾就结束。
instr('syranmo','s') 返回 1

整个语句的意思是,从iv_staff中获取decode(t.staff_id,null,'-1','','-1',t.staff_id)的位置,然后和0比较。如果iv_staff为空则从decode(t.staff_id,null,'-1','','-1',t.staff_id)中获取decode(t.staff_id,null,'-1','','-1',t.staff_id)的位置,然后和0比较。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-10
从内往外读:
1.decode(t.staff_id,null,'-1','','-1',t.staff_id)这句是,当t.staff_id是null或者无值时返回-1,其他情况返回t.staff_id的值。
2.nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id),这句是iv_staff值是null时返回第一步分析的值,否则返回iv_staff的值
3.instr(nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id))decode(t.staff_id,null,'-1','','-1',t.staff_id))这一整句是第一部中的字符串在第二部中出现的位置,最后结果是一个数值
第2个回答  2012-09-10
这个解释起来好麻烦啊,但是只要你了解oracle中instr(),decode(),nvl()这三个函数就会明白了。
首先INSTR(源字符串, 目标字符串, 起始位置, 匹配序号),像你这种只有两个参数的,后面两个就默认1:举个例子帮助理解select INSTR('CORPORATE FLOOR','OR') from dual,结果是2,也就是OR是从CORPORATE FLOOR的第二个字符开始。
2:DECODE(value, if1, then1, if2,then2, if3,then3, . . . else ).举个例子select decode(a.c_trans_type,'Q','M',a.c_trans_type),a.* from web_fin_banktranslog a 意思是如果c_trans_type这个字段的值是Q就用M代替,如果不是依然用a.c_trans_type原值。
3:NVL( string1, replace_with):这个容易解释,如果第一个字符串是null,则用replace_with代替
你那个sql用文字说起来很麻烦,但是理解这三个函数应该就很容易明白这个sql的意思了。
第3个回答  2012-09-10
instr(a,b)
表示b字符串在a中的位置
nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id))
表示如果iv_staff 为空 是nvl返回 decode的值
decode(t.staff_id,null,'-1','','-1',t.staff_id) 表示当staff_id 为null 或者 ''是 返回-1 否则返回staff_id本回答被提问者采纳
第4个回答  2012-09-10
instr(nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id)),
先从里面吧
decode(t.staff_id,null,'-1','','-1',t.staff_id))
当字段staff_id是NULL或者是空返回值是-1 否则返回staff_id
nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id)
如果字段iv_staff为NULL就返回decode(t.staff_id,null,'-1','','-1',t.staff_id))
否则返回iv_staff
instr(nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id)),decode(t.staff_id,null,'-1','','-1',t.staff_id))
表示decode(t.staff_id,null,'-1','','-1',t.staff_id)在nvl(iv_staff,decode(t.staff_id,null,'-1','','-1',t.staff_id))中的位置
相似回答