Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit f4427a6

Browse files
authored
Merge pull request #1194 from BYHAM/patch-50
CREATE TRIGGER, formatting
2 parents 6a5e0b8 + 1378267 commit f4427a6

1 file changed

Lines changed: 19 additions & 35 deletions

File tree

docs/t-sql/statements/create-trigger-transact-sql.md

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "CREATE TRIGGER (Transact-SQL) | Microsoft Docs"
33
ms.custom:
44
- "SQL2016_New_Updated"
5-
ms.date: "12/16/2016"
5+
ms.date: "04/11/2017"
66
ms.prod: "sql-non-specified"
77
ms.reviewer: ""
88
ms.suite: ""
@@ -52,7 +52,6 @@ manager: "jhubbard"
5252
## Syntax
5353

5454
```
55-
5655
-- SQL Server Syntax
5756
-- Trigger on an INSERT, UPDATE, or DELETE statement to a table or view (DML Trigger)
5857
@@ -76,7 +75,8 @@ AS { sql_statement [ ; ] [ ,...n ] | EXTERNAL NAME <method specifier [ ; ] > }
7675

7776
```
7877
-- SQL Server Syntax
79-
-- Trigger on an INSERT, UPDATE, or DELETE statement to a table (DML Trigger on memory-optimized tables)
78+
-- Trigger on an INSERT, UPDATE, or DELETE statement to a
79+
-- table (DML Trigger on memory-optimized tables)
8080
8181
CREATE [ OR ALTER ] TRIGGER [ schema_name . ]trigger_name
8282
ON { table }
@@ -93,7 +93,8 @@ AS { sql_statement [ ; ] [ ,...n ] }
9393
```
9494

9595
```
96-
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY, REVOKE or UPDATE statement (DDL Trigger)
96+
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY,
97+
-- REVOKE or UPDATE statement (DDL Trigger)
9798
9899
CREATE [ OR ALTER ] TRIGGER trigger_name
99100
ON { ALL SERVER | DATABASE }
@@ -142,7 +143,8 @@ ON { table | view }
142143

143144
```
144145
-- Windows Azure SQL Database Syntax
145-
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY, REVOKE, or UPDATE STATISTICS statement (DDL Trigger)
146+
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY,
147+
-- REVOKE, or UPDATE STATISTICS statement (DDL Trigger)
146148
147149
CREATE [ OR ALTER ] TRIGGER trigger_name
148150
ON { DATABASE }
@@ -156,9 +158,7 @@ AS { sql_statement [ ; ] [ ,...n ] [ ; ] }
156158

157159
## Arguments
158160
OR ALTER
159-
||
160-
|-|
161-
|**Applies to**: Azure [!INCLUDE[ssSDS](../../includes/sssds-md.md)], [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (starting with [!INCLUDE[ssSQL15](../../includes/sssql15-md.md)] SP1).|
161+
**Applies to**: Azure [!INCLUDE[ssSDS](../../includes/sssds-md.md)], [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] (starting with [!INCLUDE[ssSQL15](../../includes/sssql15-md.md)] SP1).
162162

163163
Conditionally alters the trigger only if it already exists.
164164

@@ -175,16 +175,12 @@ OR ALTER
175175
Applies the scope of a DDL trigger to the current database. If specified, the trigger fires whenever *event_type* or *event_group* occurs in the current database.
176176

177177
ALL SERVER
178-
||
179-
|-|
180-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
178+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
181179

182180
Applies the scope of a DDL or logon trigger to the current server. If specified, the trigger fires whenever *event_type* or *event_group* occurs anywhere in the current server.
183181

184182
WITH ENCRYPTION
185-
||
186-
|-|
187-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
183+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
188184

189185
Obfuscates the text of the CREATE TRIGGER statement. Using WITH ENCRYPTION prevents the trigger from being published as part of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] replication. WITH ENCRYPTION cannot be specified for CLR triggers.
190186

@@ -225,9 +221,7 @@ OR ALTER
225221
For INSTEAD OF triggers, the DELETE option is not allowed on tables that have a referential relationship specifying a cascade action ON DELETE. Similarly, the UPDATE option is not allowed on tables that have a referential relationship specifying a cascade action ON UPDATE.
226222

227223
WITH APPEND
228-
||
229-
|-|
230-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssKilimanjaro](../../includes/sskilimanjaro-md.md)].|
224+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssKilimanjaro](../../includes/sskilimanjaro-md.md)].
231225

232226
Specifies that an additional trigger of an existing type should be added. WITH APPEND cannot be used with INSTEAD OF triggers or if AFTER trigger is explicitly stated. WITH APPEND can be used only when FOR is specified, without INSTEAD OF or AFTER, for backward compatibility reasons. WITH APPEND cannot be specified if EXTERNAL NAME is specified (that is, if the trigger is a CLR trigger).
233227

@@ -240,9 +234,7 @@ OR ALTER
240234
After the CREATE TRIGGER has finished running, *event_group* also acts as a macro by adding the event types it covers to the sys.trigger_events catalog view.
241235

242236
NOT FOR REPLICATION
243-
||
244-
|-|
245-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
237+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
246238

247239
Indicates that the trigger should not be executed when a replication agent modifies the table that is involved in the trigger.
248240

@@ -271,18 +263,14 @@ SELECT * FROM deleted;
271263
For triggers on memory-optimized tables, the only *sql_statement* allowed at the top level is an ATOMIC block. The T-SQL allowed inside the ATOMIC block is limited by the T-SQL allowed inside native procs.
272264

273265
< method_specifier >
274-
||
275-
|-|
276-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
266+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
277267

278268
For a CLR trigger, specifies the method of an assembly to bind with the trigger. The method must take no arguments and return void. *class_name* must be a valid [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] identifier and must exist as a class in the assembly with assembly visibility. If the class has a namespace-qualified name that uses '.' to separate namespace parts, the class name must be delimited by using [ ] or " " delimiters. The class cannot be a nested class.
279269

280270
> [!NOTE]
281271
> By default, the ability of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] to run CLR code is off. You can create, modify, and drop database objects that reference managed code modules, but these references will not execute in an instance of [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] unless the [clr enabled Option](../../database-engine/configure-windows/clr-enabled-server-configuration-option.md) is enabled by using [sp_configure](../../relational-databases/system-stored-procedures/sp-configure-transact-sql.md).
282272
283-
## Remarks
284-
285-
## DML Triggers
273+
## Remarks DML Triggers
286274
DML triggers are frequently used for enforcing business rules and data integrity. [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] provides declarative referential integrity (DRI) through the ALTER TABLE and CREATE TABLE statements. However, DRI does not provide cross-database referential integrity. Referential integrity refers to the rules about the relationships between the primary and foreign keys of tables. To enforce referential integrity, use the PRIMARY KEY and FOREIGN KEY constraints in ALTER TABLE and CREATE TABLE. If constraints exist on the trigger table, they are checked after the INSTEAD OF trigger execution and before the AFTER trigger execution. If the constraints are violated, the INSTEAD OF trigger actions are rolled back and the AFTER trigger is not fired.
287275

288276
The first and last AFTER triggers to be executed on a table can be specified by using sp_settriggerorder. Only one first and one last AFTER trigger for each INSERT, UPDATE, and DELETE operation can be specified on a table. If there are other AFTER triggers on the same table, they are randomly executed.
@@ -337,7 +325,7 @@ SELECT * FROM deleted;
337325
> [!NOTE]
338326
> Because [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] does not support user-defined triggers on system tables, we recommend that you do not create user-defined triggers on system tables.
339327
340-
## DDL Triggers
328+
## Remarks DDL Triggers
341329
DDL triggers, like standard triggers, execute stored procedures in response to an event. But unlike standard triggers, they do not execute in response to UPDATE, INSERT, or DELETE statements on a table or view. Instead, they primarily execute in response to data definition language (DDL) statements. These include CREATE, ALTER, DROP, GRANT, DENY, REVOKE, and UPDATE STATISTICS statements. Certain system stored procedures that perform DDL-like operations can also fire DDL triggers.
342330

343331
> [!IMPORTANT]
@@ -444,8 +432,8 @@ GO
444432
The following example creates a DML trigger in the AdventureWorks2012 database. This trigger checks to make sure the credit rating for the vendor is good (not 5) when an attempt is made to insert a new purchase order into the `PurchaseOrderHeader` table. To obtain the credit rating of the vendor, the `Vendor` table must be referenced. If the credit rating is too low, a message is displayed and the insertion does not execute.
445433

446434
```
447-
-- This trigger prevents a row from being inserted in the Purchasing.PurchaseOrderHeader table
448-
-- when the credit rating of the specified vendor is set to 5 (below average).
435+
-- This trigger prevents a row from being inserted in the Purchasing.PurchaseOrderHeader
436+
-- table when the credit rating of the specified vendor is set to 5 (below average).
449437
450438
CREATE TRIGGER Purchasing.LowCredit ON Purchasing.PurchaseOrderHeader
451439
AFTER INSERT
@@ -506,9 +494,7 @@ GO
506494
### E. Using a server-scoped DDL trigger
507495
The following example uses a DDL trigger to print a message if any CREATE DATABASE event occurs on the current server instance, and uses the `EVENTDATA` function to retrieve the text of the corresponding [!INCLUDE[tsql](../../includes/tsql-md.md)] statement. For more examples that use EVENTDATA in DDL triggers, see [Use the EVENTDATA Function](../../relational-databases/triggers/use-the-eventdata-function.md).
508496

509-
||
510-
|-|
511-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
497+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
512498

513499
```
514500
CREATE TRIGGER ddl_trig_database
@@ -526,9 +512,7 @@ GO
526512
### F. Using a logon trigger
527513
The following logon trigger example denies an attempt to log in to [!INCLUDE[ssNoVersion](../../includes/ssnoversion-md.md)] as a member of the *login_test* login if there are already three user sessions running under that login.
528514

529-
||
530-
|-|
531-
|**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].|
515+
**Applies to**: [!INCLUDE[ssKatmai](../../includes/sskatmai-md.md)] through [!INCLUDE[ssCurrent](../../includes/sscurrent-md.md)].
532516

533517
```
534518
USE master;

0 commit comments

Comments
 (0)