轉載自:http://blog.itpub.net/30193/viewspace-364458
前幾天,公司有人因爲一個特別的事情,要把Varchar2轉換成long;
這讓我們費了不少腦精,Oracle的文檔不建議我們用Long類型的數據,但這個傢伙愣是用了,莫名其妙;
知道上週這個問題纔有了徹底解決;
我分享一個store-procedure 去闡述這個問題;
Create or replace PROCEDURE P_UPDATELOCALPLUS(strMitm in varchar2,strSubITEM in varchar2,strPlant in varchar2)
as
lLocationPlus long;
lLocationPlus01 long;
strLocation varchar2(4000);
strSubLoc varchar2(50);
lTempLoct long;
i number;
Begin
Select location,locationplus into strLocation,lLocationPlus from erpmainbom
Where parentpn=strMitm and pn=strSubITEM and plant= strPlant;
If length(strLocation)=4000 then
i:=1;
strSubLoc:=substr(strLocation,4000,i);
While InStr(strSubLoc, ' ') = 0 Loop
i := i + 1;
strSubLoc := substr(strLocation,4000-i,i);
End Loop;
--i:=i+1;
strSubLoc := substr(strLocation,4000-i,i+1);
strLocation := substr(strLocation,1, 4000 - Length(strSubLoc));
lTempLoct:=strSubLoc;
---把Varchar2的數據類型直接賦值給Long類型的數據是可以的,但不能超過4000個字節;超過,Oracle會自行截斷到4000個;那有人要問了,我多定義幾個Varchar2,不就行了?我已經試過,用多定義的合併後的Varchar2還是不能超過4000個;
End if;
lLocationPlus:=lTempLoct||lLocationPlus;
---這個代碼的是把long型數據合併,這是可以的,如果把varchar2和long型的數據一起合併,系統會報錯;
update erpmainbom set location=strLocation,
locationplus=lLocationPlus
where parentpn=strMitm and pn=strSubITEM and plant= strPlant;
這樣的update語句也是沒有問題的;用insert也一樣;
commit;
EXCEPTION
When others then
Rollback;
end;
如何把Long型數據轉換成Varchar2 呢?
用下列語句;
strLocation =substr( lLocationPlus,1,4000);