在這里先感謝一下itpub高手的帖子答復
Q:
看到的oracle中的for循環or i in 1..100 loop,i都是從1,2,3這樣遞增的,可是我這里需要i從01,02,03這樣遞增,9開始是09,10,11,12.
請問這樣的for循環怎么寫。
具體問題可見http://www.itpub.net/thread-1620912-1-1.html,我想在觸發器中加入循環變量,精簡我的觸發器長度。
ps:我是新手,謝謝大家的幫助
A:
lpad('tech', 7); would return ' tech'
lpad('tech', 2); would return 'te'
lpad('tech', 8, '0'); would return '0000tech'
lpad('tech on the net', 15, 'z'); would return 'tech on the net'
lpad('tech on the net', 16, 'z'); would return 'ztech on the net'
每次看到itpub的答復都很激動,一個恢復解決問題。
lpad函數說明
語法格式如下:
lpad( string, padded_length, [ pad_string ] )
string
准備被填充的字符串;
padded_length
填充之后的字符串長度,也就是該函數返回的字符串長度,如果這個數量比原字符串的長度要短,lpad函數將會把字符串截取成從左到右的n個字符;
pad_string
填充字符串,是個可選參數,這個字符串是要粘貼到string的左邊,如果這個參數未寫,lpad函數將會在string的左邊粘貼空格。
下面說plsq中的塊。
經常看到有人和書上直接寫declare,也不在觸發器或者存儲過程中,可是照樣使用,比如:http://zhidao.baidu.com/question/113921713.html
1,我剛開始直接在plsql中的查詢窗口sql window使用,報錯:
13:51更新:
sql windows窗口也是可以顯示的,在第二列就是output,僅僅select不行
2,想到在測試窗口test window,通過(需要點擊左上角的start debugger按鈕,然后點擊run)
-- Created on 2012-6-13 by DELL declare -- Local variables here i number; begin -- Test statements here for i in 1..100 loop dbms_output.put_line(lpad(i,2,'0')); end loop; end;
下面是結果
拓展:
1 Command window實現了SQL*Plus的所有功能,允許運行sql*plus命令,sql命令,sql腳本。
2 SQL window用於執行sql語句,顯示sql輸出,執行統計信息。(測試sql語句,查看表中的數據,更新數據)
例如 desc table(查看表結構)不能在SQL window中執行,必須在Command window中才能執行。
3 Program window中創建一個存儲過程(或者直接在plsql左邊對應的procedures和trigger右鍵新建),如下:
create or replace procedure TEST is begin DBMS_SESSION.set_nls('NLS_DATE_FORMAT','''YYYY-MM-DD HH24:MI:SS'''); DBMS_OUTPUT.PUT_LINE('HelloWorld!'); DBMS_OUTPUT.put_line(SYSDATE); end TEST;
需要注意,SET_NLS的第二個參數VALUE
輸入的值除了需要的格式外,還需要包含引號,否則會引發錯誤(選項缺失或無效)
在Command window中執行(或者在Test window中測試),如下:
set serveroutput on
exec TEST();
或者begin
2 test();
3 end;
4 / ......