MS SQL 2005 veya MS SQL 2008'de yeni eklediğim satırın id'sini nasıl alırım, bu konuda hangi komut hangi durumda kullanılmalı?

October 25, 2008 at 7:44 AMoozturk

Merhaba,

Uzun zamandır yazı yazmaya fırsatım olmuyordu, hazır zamanım varken birşeyler karalayayım dedim. Bugün maillarıma bakarken bir arkadaşım sormuş bu sorunu, ben de hazır cevap verecekken buraya yazayım dedim.

Önce kullanılabilecek komutlara ismen bakalım, sonra ne zaman ve nerelerde kullanılabileceklerine bakarız;

- @@Identity

- Ident_Current('tabloadı')

- Scope_Identity()

- Sequence

 

@@Identity

Bu komut o esnada kullanılan veritabanına herhangi bir kullanıcı tarafından, herhangi bir yolla (hatta trigger kullanıyorsanız buna triggerlar da dahil olmak üzere) en son eklenmiş olan Id'yi getirir. Kullanımında çok dikkatli olmak ve ne yaptığını çok iyi biliyor olmak gerekir. Bazı bulk copy durumlarında bir sefere mahsus olarak null'da dönebilir bu değer, bununla alakalı yazıyı da www.microsoft.com adresinde bulabilirsiniz.

 

Ident_Current('tabloadı')

@@Identity'den farklı olarak bu sefer mevcut tablo için yine herhangi bir kullanıcı, session, yol ile yaratılmış en son kaydın Id'si alınabilir.

 

Scope_Identity()

Bu komut ile mevcut bağlantı ile mevcut scope da mevcut kullanıcının herhangi bir tabloya, herhangi bir yolla eklediği en son kaydın Id'si alınabilir.

Misal vermek gerekirse:

- Bir stored procedure hazırlamış olalım ve 3 farklı tabloya 3 farklı kayıt yaratmış olalım. Bu kayıtlardan en son hangsini yarattıysak onun Id'sini Select sope_identity() ile alabiliriz. Sonra bu eklediğimiz satıra bağlı olarak ekleyeceğimiz ya da değiştireceğimiz satırları bu değerle güncelleyebilir, ekleyebiliriz.

 

Sequence

Bu komut zaten diğer SQL serverlarda mevcut idi, MS SQL'e 2005 versiyonunda eklendi. rakamsal değerlere sahip Id kolonu kullandığımızda, yeni bir satır ekleyeceğimiz zaman bu komutu çağırıp mevzu bahis tablo için bir önceki kayıttan sonraki değer'i alabiliriz. Bu sayede alacağımız değer bize ait olacağı için aynı anda başka bir komut ile aynı işlem denendiğinde aynı değer verilemeyecek ve çakışma önlenmiş olacaktır. Bu komutun geri dönüşü olmadığı için herhangi bir sebepten tabloya yazamamış ya da yazmamış olursak yine de o Id değeri alınmış ve artık kullanılamaz olmuş olur.

Bu komut ile alakalı daha fazla bilgi için yine www.microsoft.com adresine bakılabilir.

Misal vermek gerekirse:

- Bir programımız var ve birden fazla kullanıcısı var diyelim ve kullanıcılardan birden fazlası aynı anda aynı işlemi yaptığında satır eklemesi yapılacak olsun, satır eklenecek olan tablodaki son Id değeri de 2 olsun. Sequence kullandığımız için birine Id olarak 3 diğerine 4 verilecektir.

 

İyi çalışmalar dilerim.

Posted in: MSSQL

Tags: , , ,

Comments (38) -

I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Reply

After reading this I thought it was very informative. I appreciate you taking the time to put this blog piece together. I once again find myself spending way to much time both reading and commenting. What ever, it was still worth it !

Reply

Zero Nicotine is safe and easy to use. Each patch contains a dose of the most effective herbal ingredients, formulated to alleviate the symptoms of nicotine withdrawal while cleansing the system of all smoking-related toxins. To break free from smoking, you need to break free from nicotine. Unlike other stop smoking patches, Zero Nicotine’s all-natural formula contains NO NICOTINE and eliminates, rather than replenishes, the level of nicotine in your body, helping you break the habit. Used in conjunction with our proven cessation program, these Patches can help you be free of tobacco in thirty days or less.

Reply

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

Reply

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get three emails with the same comment. Is there any way you can remove me from that service? Thanks!

Regards!

Ryan Moore

Reply

I'm so thrilled with the new search of your weblog. Gratitude so a lot for keeping it running!

Reply

Good luck everybody! - I will come back again. Are you on facebook or twitter? Will like to follow you.  

Regards!


Reply

aotchpgpzledzef, <a href="http://honorar.si">zaposlitev</a>;, UaljWbozoGhRGpDCIIuA.

Reply

wewsyqmbkynkkkr, <a href="http://kathyvanzeeland.org">kathy van zeeland</a>, DgNykXIDmSIKuoNLckTN.

Reply

sjqffowcayvtowa, <a href="http://mattresstopperbuy.com">mattress topper</a>, DjWQQYSkNlcGtQCxchSF.

Reply

ztgkydzyeftufjj, <a href="http://honorar.si">zaposlitev</a>;, kIpTZghXQARwWCJcTljF.

Reply

Great website...and cool article man...thanx for the great post...keep on posting such articles... Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Reply

I didn't see a link anywhere but do you have advertising? I have several blogs in the same niche and I'd like to add my banner somwhere on your site. This site seems to have a lot of discussion and visitors.

Reply

kpeerlokpedngdhzzlxt, http://donnakaranshoes.org donna karan shoes , mtCAcWnWbhuLvENAdg. mhumqbmetxuetilytipq, http://stearnsandfoster.org stearns and foster , JrVBdeQkNQvBmchGsE.

Reply

Hello,I love reading through your blog, I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts.

Reply

Hey check out my blog too. I hope i have some fun stuff too

Reply

wxtsogzmqylmrexnotlp, http://panteneprov.org pantene pro v , BvmgVNdsEBszVMdWoP. xqryqzazquhizgignacy, http://honorar.si zaposlitev , lnTgjmXdDqRyHhoBZI.

Reply

The website article author carries a particular skill to describe actually excellent topics. Reads the web site is truly nice and you will find certainly several regular site visitors. No miracle, with all the fine content material. In any situation, it was a delight to spend time on your blog and read the interesting post.

Reply

Which dating network has the most women?

Reply

A nice post. I will visit again soon!

Reply

I do not worry about what everybody says. I love Justin Bieber and I enjoy his music. I may be a guy, but a great artist is a great artist. >.<

Reply

pwftuwncwfhjpjqwqi, http://furniturestoresinnj.org , iCKAKedAkvCaxIGeFtnJ. sqreranrfxuyxwldwl, http://kathyvanzeeland.org , FNsLiekQvrrRFMaquInA.

Reply

Whats this design called? and will it work for blogger site.

Reply

zcsrdxnwyaansxmsin, http://paulmitchellflatiron.org , CNTpIfOyTbZWyPWTsMpw. vdyqyhvysscmdinhcp, http://ellamoss.org , nxCtqIbdEqtbIJrnYuOP.

Reply

oxhvtqnpurgxjlrjge, http://verabradleybags.org , kiPmORhyCNwBYjtYvudJ. lolcvujrqjygvxcacm, http://katespade.org , IWlJdMtQeSANzdfRskUq.

Reply

Hahah, awesome. Bookmarked your website! I'll be back, definitely!

Reply

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.Any way Ill be subscribing to your feed and I hope you post again soon

Reply

I read your article.The things you have written sound very sincere and nice topics i am looking forward to its  continuation. Many of us don't know about this event. Your post is helpful.

Reply

I noticed your site whilst browsing for something distinct on Yahoo and google about topics related to movies, although I had the possibility to look over this article and I found it very useful certainly.

Reply

It was from an old friend who . thought he was dying. Anyway, he said, 'Life and death issues don't come along that often, thank God, so don't treat everything like it's life or death. Go easier.'   -   Thomas Arnold

Reply

When are you going to post again? You really entertain  a lot of people!

Reply

Hi friends. Free directory submmision with reciprocal link, submit now and get approved link directory to improve link popularity you site follow me at <A href="enhancement4male.net/linkpartners">free directory</A>. and also you can exchange link with <A href="http://enhancement4male.net/">male enhancement</A> and <A href="http://www.male-sexual.com/">penis enlargement</A> site, thank you very much.

Reply

I love the way you write and also the theme on your blog. Did you code this yourself or was it done by a professional? I'm very very impressed.

Reply

The savage bows down to idols of wood and stone the civilized man to idols of flesh and blood.   -   George Bernard Shaw

Reply

I am so grateful for your post.Much thanks again. Cool.

Reply

Astounding article, loads of handy details.

Reply

The key characted Bart acts so smart and generates these kinds of silly mistake that make him adorable.  

Reply

I'm glad you agree with a lady like me!

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading