Oracle Note01:(PL/SQL 基础)

本文汇总了 Oracle SQL 中的多种实用技巧,包括初始化应用程序、设置客户端信息、选择责任ID、调整会话语言环境等。此外还介绍了如何使用 SQL 函数进行数值运算、字符串处理、日期操作,并提供了获取子串出现次数、条件判断表达式等实用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

begin
fnd_global.apps_initialize(user_id=>2356,resp_id=>53510,resp_appl_id=>201);
end;

 begin
  dbms_application_info.set_client_info(client_info=>'235');
 end;

begin
fnd_global.apps_initialize(user_id=>2673,resp_id=>53669,resp_appl_id=>201);
end;

--- RESP_ID
SELECT responsibility_id
FROM fnd_responsibility_tl
where LANGUAGE = USERENV ('LANG') and  responsibility_id = :$PROFILES$.RESP_ID

SELECT USERENV ('LANG') FROM DUAL
alter session set nls_language='AMERICAN'---change the language environment

alter session set optimizer_goal = all_rows
alter session set optimizer_goal = rule
alter session set sql_trace = true
alter table drop column name ;
---Function --
select trunc(sysdate),trunc(sysdate)+0.9999,sysdate from dual;  --  2007/7/7,
select trunc(1.23456789,5) from dual; --1.23456 
select floor(20.54) from dual;
select abs(-12345.123) from dual;
select least(1, 2, 3) from dual ;  -- get the min number, date or char
select greatest(1, 3, 2) from dual ;  -- get the max number, date or char
select to_char(sysdate,'ddd') from dual 几天
select to_char(sysdate,'mm') from dual 几月
select decode(a,b,c)
select round(10.356789,4) from dual -- si she wu ru huo ri qi jei qu
select power(2,3) from dual -- 2*2*2 

select round(mmt.primary_quantity,2) round1,
       lpad(to_char(round(mmt.primary_quantity,2),'9999999999.99'),20,' ')  tochar
from mtl_material_transactions mmt
WHERE ROWNUM < 10;
 
select sign(:a-:b) from dual;
select length(mmt.transaction_uom) from mtl_material_transactions mmt
select rtrim(substr('asdfghjkl',1,5)),ltrim(substr('asdfghjkl',1,5)) from dual
select 1 from dual where ltrim('123852','0123456789') is null
select 1 from dual where ltrim('12385x2','0123456789') is null
SELECT NVL2 (REPLACE (NVL (TRANSLATE (LTRIM (RTRIM ('2234.4345')),
                                      '.0123456789',
                                      '00000000000'
                                     ),
                           'a'
                          ),
                      '0',
                      ''
                     ),
             'no',
             'yes'
            ) isnumber
  FROM DUAL
---Out Put --
dbms_output.put_line();
fnd_file.put_line(fnd_file.output,'XML');

-- 2 function
CREATE OR REPLACE FUNCTION f_isnumber(str1 IN VARCHAR2) RETURN NUMBER
IS
 vNumber NUMBER(38,0);
BEGIN
vNumber:=TO_NUMBER(str1);
 RETURN(1);
EXCEPTION
WHEN OTHERS THEN
 RETURN(0);
END F_ISNUMBER;
SELECT f_isnumber('12'),f_isnumber('12a') FROM dual
-- version 10
select * from test where regexp_like(name,'^[0-9A-Za-z]+$');
'^[0-9]+$',    '^[0-9A-Za-z]+$'

SELECT   (  LENGTH ('oracle vs sap! so, oracle is a winner!')
          - LENGTH (REPLACE ('oracle vs sap! so, oracle is a winner!',
                             'oracle',
                             ''
                            )
                   )
         )
       / LENGTH ('oracle') string_cnt
  FROM DUAL; --How much "subString" appear in "String". But must divide the length of "subString"

SELECT SUBSTR
          ('\\it-49\Share\cookie\55555,647437.txt',
             INSTR
                 ('\\it-49\Share\cookie\55555,647437.txt',
                  '\',
                  1,
                  (  (  LENGTH ('\\it-49\Share\cookie\55555,647437.txt')
                      - LENGTH
                            (REPLACE ('\\it-49\Share\cookie\55555,647437.txt',
                                      '\',
                                      ''
                                     )
                            )
                     )
                   / LENGTH ('\')
                  )
                 )
           + 1,
             INSTR ('\\it-49\Share\cookie\55555,647437.txt', ',')
           - INSTR
                 ('\\it-49\Share\cookie\55555,647437.txt',
                  '\',
                  1,
                  (  (  LENGTH ('\\it-49\Share\cookie\55555,647437.txt')
                      - LENGTH
                            (REPLACE ('\\it-49\Share\cookie\55555,647437.txt',
                                      '\',
                                      ''
                                     )
                            )
                     )
                   / LENGTH ('\')
                  )
                 )
           - 1
          ) COUNT
  FROM DUAL

select VENDOR_NAME ,max(Length(VENDOR_NAME))
from po_vendors
group by vendor_name
having length(VENDOR_NAME) = max(Length(VENDOR_NAME))
order by max(Length(VENDOR_NAME)) desc

union / union all / minus


SELECT * FROM ALL_OBJECTS WHERE OBJECT_NAME LIKE  '%CUST%'

SELECT MAX(ROWNUM) FROM table_name  ---find the rownum of a table

select add_months(trunc(sysdate,'MM'),-1) date_m1,
add_months(sysdate, -1) date_m2,
trunc(sysdate,'MM')    date_m3, trunc(sysdate,'YYYY'), trunc(sysdate,'DD'),
trunc(sysdate,'MM')-1  date_m4,
trunc(sysdate) date_m5, -- "trunc" select year, month and date from a date.
sysdate,
to_char(sysdate, 'YYYY-MM'), to_char(sysdate, 'MM'),
extract(year from sysdate) date_m6,
to_date(extract(year from sysdate)||'-'||extract(month from sysdate), 'YYYY-MM') date_m7
from dual
where to_char(sysdate, 'YYYY-MM') =
to_char(add_months(TO_DATE('2008-NOV-01', 'YYYY-MM-DD'), -1), 'YYYY-MM')


SELECT code_combination_id, segment2, segment3, segment4,
       (CASE
           WHEN (   segment3 || segment4 BETWEEN '70111501' AND '70111513'
                 OR segment3 || segment4 BETWEEN '70311601' AND '70311603'
                )
              THEN 'Seg'
           WHEN segment3 || segment4 IN ('71310000', '71320000')
              THEN 14
           ELSE NULL
        END
       ) case_if  ---"case ...when...then...else...end", it is just like "if...then...else...end if"
  FROM gl_code_combinations
 WHERE segment2 IN ('0311', '0360', '0362' )


--how to judge a column having char '1' ~ '9' and 'A' ~ 'Z' . the best measure is function "regexp_instr" 
select * from tbl where regexp_instr(col,'[^0-9A-Z]')=0 AND LENGTH(col)=8

----  get the week day 
select   trunc (to_date ('17/03/2009', 'DD/MM/YYYY'))
       - trunc (to_date ('09/03/2009', 'DD/MM/YYYY'), 'D') week1,
    to_char(to_date ('07/03/2009', 'DD/MM/YYYY'), 'D') week2,
       round (to_date ('09/03/2009', 'DD/MM/YYYY'), 'D') round,
       trunc (to_date ('09/03/2009', 'DD/MM/YYYY'), 'D') trunc
  from dual

insert into anny_test_oct (row_id, user_id, user_name, user_title, dept_id) values(15, 16, 'Test', 'clerk', 3);

delete from BG_EOL_LINES where EOL_LINE_ID = 10011 ;


select nvl(t.item_sub_type, 'Report') type_1,
                   decode(t.item_sub_type,
                          null,
                          'Report Total',
                          t.item_sub_type || ' Product Total:') Type_2,
                   sum(t.schedule_quantity) QTY,
                   sum(t.next_wk_qty) Next_WK_QTY,
                   count(nvl(t.item_sub_type, 'ZZZ')) * 2 rec_num
              FROM BG_MDS_DETAL_TEMP t
             group by rollup(t.item_sub_type)

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9182041/viewspace-630930/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9182041/viewspace-630930/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值