MSSQL Trigger Using

M3m0ry

Kıdemli Üye
3 Haz 2017
4,410
124
3
xD
Hi TurkHackTeam members.

I'll answer What is Trigger ? Why we use Trigger ? and How to Use Trigger ? questions to you today.

What Is Trigger ?
GeB2yR.png


This trigger process can be occured before or after any process.

  • Transfering of deleted datas from table to other table.
  • They realize on virtual tables
  • Insterted table for adding
  • Deleted table for delete processes
  • There is no update table for updates. All update processes are a instert process. So, we are fallowing inserted or deleted table.
  • While Trigger is on process (adding or delete processes), can revoke that process for block to data adding or deleting.

In Which Situations Triggers Step In?

GeB2yR.png


"After Trigger" or "For Trigger" step in after stated process

Using
GeB2yR.png


IQ4Py6.png


Note; After and Instead Of are making same processes

Try on Examples
GeB2yR.png


I'll try processes on Nortwind as example database. Tables are related. So, i will copy one of them with bulk instert.

Kod:
SELECT * INTO YedekCustomers FROM dbo.Customers c

Now write a trigger over "YedekCustomers"

0d8Txy.png


Kod:
CREATE TRIGGER DelCustomers
ON YedekCustomers
AFTER DELETE
AS
BEGIN
	ROLLBACK
END

We wrote a trigger for rollback. We wrote rollback because we want to block delete processes. Lets try to delete.

3PPVUQ.png


As you can see, we got an error message. Our process blocked by trigger.

Note: Delete command blocks by trigger. But If we want to reset all table with Truncate, our trigger wont block that.

We can use that for uptade and insert processes too. You can write update and Instert instead of Delete for change process.

Disable Trigger

GeB2yR.png


For example; we blocked an user's deleting, inserting or updating. I am a database programmer. I need to make a process on this table over Backend. Will i must delete and create a repeatedly? Of course not.

OfT75I.png


Look, i deleted saved which is have CutomerId. And worked.

NSG4cS.png


If we dont know trigger name, or we need to finish our process quickly, we can make like this;

Kod:
DISABLE	TRIGGER ALL
DELETE FROM YedekCustomers WHERE dbo.YedekCustomers.CustomerID = 'ALFKI'
ENABLE	TRIGGER ALL

We will backup deleted datas
GeB2yR.png


Td062z.png


We will write trigger
GeB2yR.png


915U3I.png


Now we are testing it
9faQMK.png


As you can see, that process finished succesfully.

Source: https://www.turkhackteam.org/databases/1916823-mssql-trigger-kullanimi.html
Translator: M3m0ry
 
Moderatör tarafında düzenlendi:
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.