• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in using triggers and sequences

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an employee table, whose primary key , empId, is auto-generated.
I am using following sequence:


And I amusing this sequence to generate auto-id in a trigger which is fired when an insert in the employee table is made:


But I am getting auto-id like 0308 0001 instead of 03080001

Can I know what is the reason behind that and how to remove that space?
Thanks in advance
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I played with this some and noticed Oracle always put a leading space on the sequence mask . Should be able to use LTRIM to get rid of it:

select mon || yr || LTRIM(to_char(empid_seq.nextval,'0009'),' ') into :new.empId from dual;
 
Pranav Pal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Agador Paloi:
I played with this some and noticed Oracle always put a leading space on the sequence mask



I also played like you and got the same conclusion and solution

Thanks...
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also find the leading space after converting the sequence to a character!?

I thought that if the empID in the table is a NUMBER datatype it may be better to explicitly cast the generated key value into a NUMBER. Any error generated when trying to convert to number could be handled in an exception and then calculate and return a valid number - also perhaps it is uneccessary to declare the local variables and we could then make only one call to the SQL engine;


[ March 31, 2008: Message edited by: Craig Collins ]
 
Craig Collins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the oracle docs,

Oracle uses blank characters to fill format elements to a constant width equal to the largest element for the relevant format model in the current session language.



I'm guessing to accomodate the (possible) minus sign oracle pads the 4 digit format model to 5 characters!

So a solution using TO_CHAR is to use a Format Model Modifier to control this blank padding.




Could also use LPAD - LPAD definition in Oracle SQL Language Reference



lpad(string, length of string returned, character to use to pad).

lpad return a string of either varchar2, nvarchar2, or LOB depending on the datatype of your string.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic