Function sharing of oracle generating dynamic prefix and self increasing number
- 2021-11-30 01:52:34
- OfStack
create or replace
Function GetInvitationNO(prev varchar2, num1 varchar2, num2 varchar2, sessionSetting varchar2)
Return Varchar2
Authid Current_User Is PRAGMA AUTONOMOUS_TRANSACTION;-- Need to use " Current_User "The permissions of prevent cannot run" Execute Immediate "Command & " PRAGMA AUTONOMOUS_TRANSACTION "Self-made transaction prevention DML Unable to DDL The question of
Totalprev Varchar2(50);
Hassequences Number;
ReNO varchar2(50);
Begin
-- Prefix of number
Totalprev := Prev || Num1 || Num2 || Sessionsetting;
-- Create sequence
Select Count(*) Into Hassequences From User_Sequences Where Sequence_Name=Totalprev;
If Hassequences <> 1 Then
Execute Immediate 'Create Sequence '||Totalprev||' Increment By 1 Start With 1 maxvalue 9999999 Nocycle';
End If;
-- The sequence value is obtained by dynamic sequence name, and the obtained sequence value is converted into 7 Bit length string, which is not long enough " 0 "Fill
Execute Immediate 'Select '''|| Totalprev ||''' || to_char('||Totalprev||'.Nextval,''FM0000000'') From Dual' into ReNO;
-- to_char() Medium FM Heeled 0 Represents the number of digits, and the shortage is used " 0 "Fill in; Add" FM "You can avoid the appearance of spaces, which are reserved for sign bits. If you use them for negative numbers, to_char The space part is used to display-, because it is a positive number, so it does not display +, but is replaced by a space
Return ReNO;
end;
Test
Select GetInvitationNO('p', '1', '01', '114') From dual
-- Dual Yes Oracle In 1 An actual table that can be read by any user, and is commonly used in a table without a target table select Statement block