{"id":160,"date":"2019-09-30T22:18:39","date_gmt":"2019-09-30T19:18:39","guid":{"rendered":"http:\/\/www.potansif.com\/?p=160"},"modified":"2019-09-30T22:18:39","modified_gmt":"2019-09-30T19:18:39","slug":"sql-server-trigger-hangi-kipte-calisiyor","status":"publish","type":"post","link":"http:\/\/www.potansif.com\/?p=160","title":{"rendered":"SQL Server : Trigger hangi kipte \u00e7al\u0131\u015f\u0131yor"},"content":{"rendered":"<p>Siz de benim gibi baz\u0131 hesaplamalar\u0131 veritaban\u0131na kaydetme s\u0131ras\u0131nda yapmay\u0131 sevenlerdenseniz veya b\u00f6yle bir ihtiyac\u0131n\u0131z varsa s\u0131kl\u0131kla bu i\u015fleri Trigger&#8217;ler \u00fczerinden halletmeniz gerekir.<br \/>\n<!--more--><br \/>\nSQL Server&#8217;de bir Trigger&#8217;in hangi modda oldu\u011funu o an i\u00e7in bize s\u00f6yleyen bir i\u015flev yok. Bunu \u00f6\u011frenebilmek i\u00e7in INSERTED ve DELETED tablolar\u0131n\u0131n dolu olup olmad\u0131\u011f\u0131na bakman\u0131z ve bir \u00e7\u0131karsama yapman\u0131z gerekir. Bu i\u015fi kolayla\u015ft\u0131ran bir fonksiyon yazm\u0131\u015ft\u0131m, sizlerle payla\u015fman\u0131n faydal\u0131 olaca\u011f\u0131na inan\u0131yorum;<\/p>\n<pre><code>SET ANSI_NULLS ON\nGO\nSET QUOTED_IDENTIFIER ON\nGO\n-- =============================================\n-- Author: &nbsp; &nbsp; &nbsp;<u\u011fur parlayan=\"\">\n-- Create date: &lt;16.12.2014 12:46&gt;\n-- Description: &nbsp;<trigger'lerin hangi=\"\" kipte=\"\" \u00e7al\u0131\u015ft\u0131\u011f\u0131n\u0131=\"\" anlamak=\"\" i\u00e7in=\"\" kullan\u0131l\u0131r.=\"\">\n-- =============================================\n--CREATE FUNCTION dbo.fn_triggerkipi\n&nbsp;ALTER FUNCTION dbo.fn_triggerkipi\n&nbsp;( @InsertedCount &nbsp;INT = 0 --&gt; &nbsp;\u00d6rnek (SELECT TOP 1 COUNT(*) FROM INSERTED)\n&nbsp;, @DeletedCount &nbsp; INT = 0 --&gt; &nbsp;\u00d6rnek (SELECT TOP 1 COUNT(*) FROM DELETED)\n&nbsp;)\nRETURNS VARCHAR(8)\nAS &nbsp;BEGIN\n&nbsp; &nbsp;RETURN\n&nbsp; &nbsp;CASE\n&nbsp; &nbsp; &nbsp;WHEN @InsertedCount &gt; 0 AND @DeletedCount = 0 THEN 'ekle' &nbsp; &nbsp; &nbsp;--&gt; INSERT Kipidir, DELETED tablosu bo\u015f gelir...\n&nbsp; &nbsp; &nbsp;WHEN @InsertedCount &gt; 0 AND @DeletedCount &gt; 0 THEN 'de\u011fi\u015ftir' &nbsp;--&gt; UPDATE Kipidir, Her iki tablo da doludur... DELETED tablosu \u00f6nceki veriyi, INSERTED ise yeni veriyi tutar...\n&nbsp; &nbsp; &nbsp;WHEN @InsertedCount = 0 AND @DeletedCount &gt; 0 THEN 'sil' &nbsp; &nbsp; &nbsp; --&gt; DELETE Kipidir, INSERTED tablosu bo\u015f gelir...\n&nbsp; &nbsp; &nbsp;ELSE 'yok' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --&gt; Hi\u00e7biri &nbsp; &nbsp; &nbsp; , Her iki tablo da bo\u015ftur... (Ki asl\u0131nda parametre hatal\u0131ysa b\u00f6yle bir durum olu\u015fur)\n&nbsp; &nbsp;END;\nEND<\/trigger'lerin><\/u\u011fur><\/code><\/pre>\n<p>Kullan\u0131m\u0131 olduk\u00e7a basittir ve genel anlamda s\u0131k s\u0131k trigger&#8217;ler ile u\u011fra\u015f\u0131yorsan\u0131z bu bahsetti\u011fim ad\u0131m\u0131 basite indirgemenizde size yard\u0131mc\u0131 olacakt\u0131r.<\/p>\n<pre><code>SET ANSI_NULLS ON\nGO\nSET QUOTED_IDENTIFIER ON\nGO\n-- =============================================\n-- Author &nbsp; &nbsp; &nbsp;: <u\u011fur parlayan=\"\">\n-- Create date : &lt;2017-02-20&gt;\n-- Description : &lt;...&gt;\n-- =============================================\nALTER TRIGGER &nbsp;[dbo].[TRG_Ajanda]\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ON &nbsp;[dbo].[Ajanda]\nFOR INSERT, DELETE, UPDATE\nAS &nbsp;BEGIN\n&nbsp; &nbsp;\n&nbsp; &nbsp;SET NOCOUNT ON;\n&nbsp; &nbsp;\n&nbsp; &nbsp;DECLARE @TRG_Durum &nbsp;VARCHAR(8) = dbo.fn_triggerkipi( (SELECT TOP 1 COUNT(*) FROM INSERTED), (SELECT TOP 1 COUNT(*) FROM DELETED) )\n&nbsp; &nbsp;\n&nbsp; &nbsp;IF (@TRG_Durum = 'ekle')\n&nbsp; &nbsp;or (@TRG_Durum = 'de\u011fi\u015ftir') BEGIN\n\n&nbsp; &nbsp; &nbsp; &nbsp;UPDATE &nbsp;X\n&nbsp; &nbsp; &nbsp; &nbsp;SET &nbsp; &nbsp; X.Taslak = ISNULL(I.Taslak, 0)\n&nbsp; &nbsp; &nbsp; &nbsp;FROM &nbsp; &nbsp;dbo.Ajanda &nbsp; &nbsp;as &nbsp;X &nbsp; &nbsp; &nbsp;LEFT OUTER\n&nbsp; &nbsp; &nbsp; &nbsp;JOIN &nbsp; &nbsp;INSERTED &nbsp; &nbsp; &nbsp;as &nbsp;I &nbsp;on &nbsp;I.Ref = X.Ref\n&nbsp; &nbsp; &nbsp; &nbsp;WHERE &nbsp; X.Ref in (SELECT DISTINCT Ref FROM INSERTED)\n&nbsp; &nbsp; &nbsp; &nbsp;;\n&nbsp; &nbsp;END ELSE\n&nbsp; &nbsp;IF&nbsp;(@TRG_Durum = 'sil') BEGIN\n&nbsp; &nbsp; &nbsp; &nbsp;SET NOCOUNT ON; --&gt; bu sat\u0131r\u0131n yerine silme i\u015flemi s\u0131ras\u0131nda bir log tutuyorsan\u0131z ilgili kodlar\u0131n\u0131z\u0131 bu noktaya yazabilirsiniz...\n&nbsp; &nbsp;END\nEND<\/u\u011fur><\/code><\/pre>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc gibi 17. sat\u0131rda (@TRG_Durum) de\u011fi\u015fkenine hangi modda \u00e7al\u0131\u015ft\u0131\u011f\u0131 i\u015fleniyor ve devam\u0131ndaki IF blo\u011funda da mevcut duruma g\u00f6re SQL kodlar\u0131n\u0131z\u0131 birbirine kar\u0131\u015ft\u0131rmadan yazabiliyorsunuz.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Siz de benim gibi baz\u0131 hesaplamalar\u0131 veritaban\u0131na kaydetme s\u0131ras\u0131nda yapmay\u0131 sevenlerdenseniz veya b\u00f6yle bir ihtiyac\u0131n\u0131z varsa s\u0131kl\u0131kla bu i\u015fleri Trigger&#8217;ler \u00fczerinden halletmeniz gerekir.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[11,7,20],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-blog","tag-programlama","tag-sql-server","tag-trigger"],"_links":{"self":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":1,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"predecessor-version":[{"id":161,"href":"http:\/\/www.potansif.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions\/161"}],"wp:attachment":[{"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.potansif.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}