NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

--Trigger oluşturma

--İnsert trigger

create trigger HesapEklemeDurumu
on Hesap
after insert --trigger tabloya giriş işlemi yapıldıktan sonra
as
begin
set nocount on
select 'yeni kayıt ekledi'
set nocount off
end


insert into Hesap(Adi,Soyad,Bolum,Fiyat)
values('Ali','Tek',1,500)


create trigger trg_PersonelH
on Personeller
after insert
as
raiserror ('Eklenen personeli bildir' ,16,10)

insert into Hesap(Adi,Soyad,Bolum,Fiyat)
values('Ali','Tek',1,500)

INSERT INTO [dbo].[Personeller]
([PpersonelID]
,[ADI]
,[SOYADI]
,[MESLEK])
VALUES
(10,'ali','veli','issiz')


--girilen tablo değerini alıp başka bir tabloya eş zamanlı yazma

create table HesapLog
([PersonelID] int
,[Adi]



) --eksik

--after insert -->

create trigger trg_GetProduct
on Production.Product
after insert
as
begin

select PP.ProductID,PP.Name,PP.ProductNumber from Production.Product
as PP inner join inserted as I on PP.ProductID=I.ProductID

end

insert into Production.Product (Name,ProductNumber,MakeFlag ,FinishedGoodsFlag,ReorderPoint,
StandardCost,ListPrice,DaysToManufacture,SellStartDate,rowguid,ModifiedDate)
values('Smart Kit','SM-301',1,0,50,700,0.00,0.00,3,GETDATE(),NEWID(),GETDATE());

SELECT * FROM Production.Product where ProductNumber='Smart Kit'

create trigger trtr_KullaniciSil
on Personeller
after delete --silinme işlemi yapılınca
as
begin

select deleted.FirstName+' '+deleted.LastName+' kişisi silindi') as SilinenKisi
from deleted --silinen geçici tablo üzerinden işlem yaptık
end

select * from dbo.Personeller

delete from Personeller where BusinessEntityID=265

--Update trigger

create trigger trgr_Guncelle on Production.Product
after update as begin
Update Production.Product set ModifiedDate=GETDATE() where ProductID=(select ProductID from inserted)

end

select * from Production.Product where ProductID=2

select * from Production.Product



create table GuncellenenUrunler(
ProductID int,
Name varchar(50),
ProductNumber nvarchar(25),
ListPrice money,
ModifiedDate datetime
)

create trigger trgr_guncellenenUrunLog
on Production.Product
after update
as
begin

--değişkenler
declare
@ProductID int,
@Name varchar (50),
@ProductNumber nvarchar(25),
@ListPrice money,
@ModifiedDate datetime

--değişkenlere değer atama
select
@ProductID=ins.ProducutID,
@Name=ins.Name,
@ProductNumber=ins.ProductNumber,
@ListPrice=ins.ListPrice,
@ModifiedDate =ins.ModifiedDate

from inserted as ins
--GuncellenenUrunler veri girişi yapıldı
insert into GuncellenenUrunler
values(@ProductID,@Name,@ProductNumber,@ListPrice,@ModifiedDate )

end

update Production.Product
set Name='Decal one' where ProductID=325

select * from Production.Product

select*from GuncellenenUrunler

truncate table GuncellenenUrunler



create table SilinenUrunler(
ProductID int,
Name varchar(50),
ProductNumber nvarchar(25),
ListPrice money,
ModifiedDate datetime
)

create trigger trgr_silinenUrunLog
on Production.Product
after update
as
begin

--değişkenler
declare
@ProductID int,
@Name varchar (50),
@ProductNumber nvarchar(25),
@ListPrice money,
@ModifiedDate datetime

--değişkenlere değer atama
select
@ProductID=ins.ProductID,
@Name=ins.Name,
@ProductNumber=ins.ProductNumber,
@ListPrice=ins.ListPrice,
@ModifiedDate =ins.ModifiedDate

from deleted as ins
--GuncellenenUrunler veri girişi yapıldı
insert into SilinenUrunler
values(@ProductID,@Name,@ProductNumber,@ListPrice,@ModifiedDate )

end

delete from Production.Product where ProductID=325

select *from SilinenUrunler



---Birden fazla işlem için Trigger oluşturmak

create trigger trgr_Tumu
on Hesap
after insert,update,delete




create table Siparis2(

SiparisID int identity not null primary key,
MusteriID nvarchar(50) not null,
SiparisTarih datetime not null
)

create table Urunler2(

UrunID int identity not null primary key,
Ad nvarchar(50),
BirimFiyat money not null

)

create table SiparisUrunler2(

SiparisID int not null references Siparis2(SiparisID),
UrunID int not null references Urunler2(UrunID),
BirimFiyat money not null,
Miktar int not null

constraint PKSiparisUrun2 primary key clustered(SiparisID,UrunID) --İki sütun içinde clustered index oluşturuyoruz.
)

create table Musteri1(

MusteriID int not null primary key,
Ad varchar(40) not null
)

insert into Musteri1 values(1,'mobilveri bilişim')
insert into Musteri1 values(2,'Smartpro bilgisayar')

insert into Siparis2 values (1,GETDATE());
insert into Siparis2 values (2,GETDATE());

insert into Urunler2 values ('Developer Book',150)
insert into Urunler2 values ('Frontend Book',100)

insert into SiparisUrunler2 values (1,1,150,3)
insert into SiparisUrunler2 values (2,2,100,2)




create view vm_MusteriSiparisleri1
as
select SP.SiparisID,SU.UrunID,U.Ad,SU.BirimFiyat,SU.Miktar,SP.SiparisTarih

from Siparis2 as SP join SiparisUrunler2 as SU on SP.SiparisID=SU.SiparisID
join Urunler2 as U on SU.UrunID =U.UrunID

select * from vm_MusteriSiparisleri1


create trigger tg_MusteriSiparisEkle1
on vm_MusteriSiparisleri1
instead of insert
as
begin
set nocount on
if((select count(*) inserted)>0)
begin
insert into SiparisUrunler2
select i.SiparisID, i.UrunID,i.BirimFiyat ,i.Miktar
from inserted as i join Siparis2 as S on i.SiparisID=S.SiparisID


if @@ROWCOUNT=0
raiserror ('eşleşme yok, eklemeyapılmadı',1,01)
end

set nocount off

end;

insert into vm_MusteriSiparisleri1(SiparisID,UrunID,Ad,BirimFiyat,Miktar,SiparisTarih)
values (1,2,'Kalem',5,5,'2019-12-3')


select* from Siparis2
select* from Urunler2
select* from SiparisUrunler2
select* from Musteri1


---inserted of update trigger

create trigger tgInstedOfTrigger1
on Person.Person
instead of update
as
begin

print 'Güncelleme yapılmak istendi'

end

update Person.Person
set LastName='Demo' where BusinessID=1
SELECT * from Person.Person where BusinessID=1

--instead if delete trigger
---Hemen hemen tüm sütunlara ekelnir 0 ları getir 1 leri getirme
alter table Urunler2
add AktifMi BIT

select * from Urunler2

Update Urunler2 set AktifMi=1


alter view vm_MusteriSiparisleri1
as
select SP.SiparisID,SU.UrunID,U.Ad,SU.BirimFiyat,SU.Miktar,SP.SiparisTarih,U.AktifMi

from Siparis2 as SP join SiparisUrunler2 as SU on SP.SiparisID=SU.SiparisID
join Urunler2 as U on SU.UrunID =U.UrunID

select * from vm_MusteriSiparisleri


create trigger trg_UrunuYayindanKaldir
on vm_MusteriSiparisleri1
instead of delete
as begin
set nocount on
if((select count(*) from deleted)>0)
begin
declare @ID int
select @ID=UrunID from deleted

update vm_MusteriSiparisleri
set AktifMi=0 where UrunID=@ID;
if @@RowCount=0
raiserror('Eşleme yok,Kapatma işlemi yapılmadı',10,1)
end
set nocount off
end


--AktifMi 1 olanı silip tekrardan 0 olarak getirdi çıktıda 1 0 oldu
delete from vm_MusteriSiparisleri where UrunID=1
select * from Urunler2



--AktifMi 0 ı 1 yap.
create trigger trg_UrunuYayinaKoy
on vm_MusteriSiparisleri1
instead of update
as begin
set nocount on
if((select count(*) from inserted)>0)
begin
declare @ID int
select @ID=UrunID from inserted

update vm_MusteriSiparisleri
set AktifMi=1 where UrunID=@ID;

end
if @@RowCount=0
raiserror('Eşleme yok,Kapatma işlemi yapılmadı',10,1)

set nocount off
end

update vm_MusteriSiparisleri
set AktifMi=1 where UrunID=1




--if update() ve columsn_updated()

--Bu işlev, bir tablonun veya görünüm eklenen veya güncellenmiş sutunlarını
--gösteren değişken bir bit deseni döndürür.Tetikleyici belirli eylemleri
--gerçekleştirip gerçekleştirmemesi gerektiğini test etmek için Transact-SQL
--insert veya update tetikleyicinin gövdesinin herhangi bir yerinde
--Columns_updated kullanılır.

--update() fonksiyonu
create trigger Trg_ProductNumberControl
on Production.Product
after update
as
begin

if update(ProductNumber) --güncelleme kontrolü
begin
print 'ProductNumber değeri değiştirilmez'
rollback --işlemi geri al
end

end



go
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.