﻿
{"id":5428,"date":"2016-12-08T06:54:09","date_gmt":"2016-12-08T06:54:09","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=5428"},"modified":"2016-12-08T06:54:09","modified_gmt":"2016-12-08T06:54:09","slug":"transact-sqlt-sqlde-identity-scope_identity-ve-ident_current-komutlari","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/transact-sqlt-sqlde-identity-scope_identity-ve-ident_current-komutlari\/","title":{"rendered":"Transact SQL(T-SQL)&#8217;de @@IDENTITY, SCOPE_IDENTITY() ve IDENT_CURRENT() Komutlar\u0131"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>T-SQL&#8217;de identity kolonlarla \u00e7al\u0131\u015f\u0131rken yeni \u00fcretilen de\u011ferleri elde etmemiz gerekebilir. \u0130\u015fte bu ihtiyaca d\u00f6n\u00fck @@IDENTITY komutu yahut SCOPE_IDENTITY() ve IDENT_CURRENT() fonksiyonlar\u0131 kullan\u0131labilir. Peki bu \u00fc\u00e7 komut aras\u0131ndaki temel farklar nelerdir? gelin ortaya \u00e7\u0131karal\u0131m.<\/p>\n<h4><span style=\"color: #800080;\">@@IDENTITY<\/span><\/h4>\n<p>A\u00e7\u0131lm\u0131\u015f olan ba\u011flant\u0131da(connection) tablo yahut sorgunun \u00e7al\u0131\u015ft\u0131\u011f\u0131 scope&#8217;a bakmaks\u0131z\u0131n son \u00fcretilen identity de\u011ferini vermektedir.<\/p>\n<p><em>(Dikkat!!! Trigger kullan\u0131lan sorgularda yanl\u0131\u015f sonu\u00e7 alma ihtimalinden dolay\u0131 kullan\u0131lmas\u0131 tavsiye edilmez.)<\/em><\/p>\n<h4><span style=\"color: #800080;\">SCOPE_IDENTITY<\/span><\/h4>\n<p>A\u00e7\u0131lm\u0131\u015f olan ba\u011flant\u0131da(connection) ve sorgunun \u00e7al\u0131\u015ft\u0131\u011f\u0131 scope&#8217;ta son \u00fcretilen identity de\u011ferini d\u00f6nd\u00fcr\u00fcr.<\/p>\n<p>(<em>Dikkat!!! Trigger kullan\u0131lan sorgularda @@IDENTITY yerine bu fonksiyonun kullan\u0131lmas\u0131 tavsiye edilir.<\/em>)<\/p>\n<h4><span style=\"color: #800080;\">IDENT_CURRENT(TabloAdi)<\/span><\/h4>\n<p>Ba\u011flant\u0131 ve sorgunun \u00e7al\u0131\u015ft\u0131r\u0131ld\u0131\u011f\u0131 scope&#8217;a bakmaks\u0131z\u0131n parametre olarak verilen tabloda \u00fcretilen sonuncu identity de\u011ferini d\u00f6nd\u00fcr\u00fcr.<\/p>\n<p>\u015eimdi bu \u00fc\u00e7 farkl\u0131 komut \u00fczerine a\u015fa\u011f\u0131daki \u00f6rne\u011fi inceleyelim.<br \/>\nVeritaban\u0131 ve tablolar\u0131m\u0131z&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nCREATE DATABASE ORNEKVERITABANI\r\n\r\nCREATE TABLE ORNEKTABLO1\r\n(\r\n\tID INT PRIMARY KEY IDENTITY,\r\n\tKOLON1 NVARCHAR(MAX),\r\n\tKOLON2 NVARCHAR(MAX)\r\n)\r\n\r\nCREATE TABLE ORNEKTABLO2\r\n(\r\n\tID INT PRIMARY KEY IDENTITY,\r\n\tKOLON1 NVARCHAR(MAX),\r\n\tKOLON2 NVARCHAR(MAX)\r\n)\r\n<\/pre>\n<p>Trigger\u0131m\u0131z&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nUSE ORNEKVERITABANI\r\n\r\nCREATE TRIGGER KONTROL\r\nON ORNEKTABLO1 FOR INSERT\r\nAS\r\nINSERT ORNEKTABLO2 VALUES('', '')\r\n<\/pre>\n<p>Sorgular\u0131m\u0131z&#8230;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nINSERT ORNEKTABLO2 VALUES('1','1')\r\nINSERT ORNEKTABLO2 VALUES('2','2')\r\nINSERT ORNEKTABLO2 VALUES('3','3')\r\nINSERT ORNEKTABLO2 VALUES('4','4')\r\nINSERT ORNEKTABLO2 VALUES('5','5')\r\n\r\nINSERT ORNEKTABLO1 VALUES('6','6')\r\n\r\nSELECT @@IDENTITY\r\nUNION ALL\r\nSELECT SCOPE_IDENTITY()\r\nUNION ALL\r\nSELECT IDENT_CURRENT('ORNEKTABLO1')\r\n<\/pre>\n<p>Sonu\u00e7 olarak&#8230;<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/Transact-SQLT-SQLde-@@IDENTITY-SCOPE_IDENTITY-ve-IDENT_CURRENT-Komutlar\u0131.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2016\/12\/Transact-SQLT-SQLde-@@IDENTITY-SCOPE_IDENTITY-ve-IDENT_CURRENT-Komutlar\u0131.png\" alt=\"Transact SQL(T-SQL)&#039;de @@IDENTITY, SCOPE_IDENTITY() ve IDENT_CURRENT() Komutlar\u0131\" width=\"140\" height=\"100\" class=\"aligncenter size-full wp-image-5432\" \/><\/a><br \/>\nDikkat ederseniz @@Identity komutu bir tabloya 5 kay\u0131t di\u011fer tabloya 1 kay\u0131t ger\u00e7ekle\u015ftirdi\u011fi halde 6 olarak g\u00f6stermektedir. Bu y\u00fczden trigger kulland\u0131\u011f\u0131m\u0131z sorgularda @@identity komutunu tavsiye etmemekteyiz. SCOPE_IDENTITY fonksiyonu \u00e7al\u0131\u015ft\u0131r\u0131lan scope&#8217;da son \u00fcretilen identity de\u011ferinin ORNEKTABLO1 tablosunda ilk kay\u0131t olmas\u0131ndan dolay\u0131 1 de\u011ferini verdi\u011fini g\u00f6rmekteyiz. Ayriyetten IDENT_CURRENT fonksiyonuda ilgili tablo i\u00e7erisindeki son de\u011feri vermektedir.<\/p>\n<p>G\u00f6rd\u00fc\u011f\u00fcn\u00fcz gibi ayn\u0131 i\u015fi farkl\u0131 y\u00f6ntem ve i\u015fleyi\u015flerle yapan \u00fc\u00e7 farkl\u0131 komutturlar.<\/p>\n<p>Sonraki yaz\u0131lar\u0131m\u0131zda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar dilerim&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>T-SQL&#8217;de identity kolonlarla \u00e7al\u0131\u015f\u0131rken yeni \u00fcretilen de\u011ferleri elde etmemiz gerekebilir. \u0130\u015fte bu ihtiyaca d\u00f6n\u00fck @@IDENTITY komutu yahut SCOPE_IDENTITY() ve IDENT_CURRENT() fonksiyonlar\u0131 kullan\u0131labilir. Peki bu \u00fc\u00e7 komut aras\u0131ndaki temel farklar nelerdir? gelin ortaya \u00e7\u0131karal\u0131m. @@IDENTITY&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1082,22],"tags":[100,1563,1562,461,85],"class_list":["post-5428","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","category-veritabani","tag-identity","tag-ident_current","tag-scope_identity","tag-t-sql","tag-transact-sql"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5428","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=5428"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/5428\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3415"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=5428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=5428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=5428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}