• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

help with SQL Server

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, this is a pure tsql and SQL Server issue. If this is not the right place, or non java matters can't be discussed in the Ranch, sorry for the inconvenience.

I need to build a table with an specific kind of primary key. The table is 'person':


Here is the matter: the primary key (id) will be the initial letter of the name + the initial letter of the city + the age + the identity number (cont). For example, when I insert ('eduardo', 'araraquara', 19) the generated PK will be 'ea191'. the second record ('bear', 'ranch', 30) will be 'br302', and so on.

I have never worked with triggers, until I was told this can be done with it. I managed to create this one:


But when I insert the above records in the table I get the following IDs: 'ea190', 'br300'. As you can see, the command 'SELECT cont FROM inserted' is returning the value 0. The identifier is being generated after the record is inserted. Does anyone know how can I work on this issue? Recall that I don't know much about triggers, if I'm doing anything wrong please notify me. I hope the matter is quite clear and understanding.
 
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know your answer, but you appear to have posted in the correct place. You are however more likely to get good answers if you let people know what the thread is about
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using @@indentity instead of the value from inserted. This should be the last identity value used.
 
A feeble attempt to tell you about our stuff that makes us money
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic