cheap nfl jerseys china cheap nfl jerseys free shipping wholesale nfl jerseys china wholesale jerseys from china cheap nfl jerseys free shipping cheap nfl jerseys for sale cheap jerseys free shipping wholesale nfl jerseys from china cheap nfl jerseys sale cheap nike nfl jerseys china wholesale jerseys free shipping cheap nfl jerseys wholesale wholesale nfl jerseys online cheap nfl jerseys wholesale china jerseys wholesale cheap coach handbags outlet authentic designer handbags cheap coach handbags outlet cheap coach purses outlet discount coach bags coach bags sale coach purse outlet cheap real coach purses coach handbags sale online coach purse outlet michael kors outlet online store cheap michael kors bags cheap michael kors purse michael kors factory outlet online cheap michael kors handbags cheap michael kors purses michael kors bags outlet online cheap michael kors purse michael kors handbags discount cheap michael kors purse michael kors handbags discount
cheap nfl jerseys china cheap nfl jerseys free shipping wholesale nfl jerseys china wholesale jerseys from china cheap nfl jerseys free shipping cheap nfl jerseys for sale cheap jerseys free shipping wholesale nfl jerseys from china cheap nfl jerseys sale cheap nike nfl jerseys china wholesale jerseys free shipping cheap nfl jerseys wholesale wholesale nfl jerseys online cheap nfl jerseys wholesale china jerseys wholesale cheap coach handbags outlet authentic designer handbags cheap coach handbags outlet cheap coach purses outlet discount coach bags coach bags sale coach purse outlet cheap real coach purses coach handbags sale online coach purse outlet michael kors outlet online store cheap michael kors bags cheap michael kors purse michael kors factory outlet online cheap michael kors handbags cheap michael kors purses michael kors bags outlet online cheap michael kors purse michael kors handbags discount cheap michael kors purse michael kors handbags discount
triggers

stored procedures che vanno automaticamente in esecuzione immediatamente dopo che una tabella viene modificata

realizzano la consistenza specifica di ogni singola applicazione, infatti consentono:



visualizzazione di un messaggio

CREATE TRIGGER attenzione ON titoli FOR INSERT, UPDATE AS
RAISERROR (50009,16,10)


copia di record cancellati

CREATE TRIGGER salva ON titoli FOR DELETED AS
INSERT copia SELECT * FROM deleted


mantenimento di totali in inserimento o cancellazione
se le righe inserite sono pi� di una, si deve usare la clausola GROUP BY, che per� � molto pesante, per cui � preferibile controllare il numero di righe inserite:

CREATE TRIGGER totali ON vendite FOR INSERT AS
IF @@rowcount = 1
	UPDATE titoli
	SET vendite_annue = vendite_annue + quantit�
	FROM inserted WHERE titoli.titolo = inserted.titolo
ELSE
	UPDATE titoli
	SET vendite_annue = vendite_annue + (SELECT SUM(quantit�)
	GROUP BY inserted.titolo
	FROM inserted WHERE titoli.titolo = inserted.titolo)


inserimento condizionato
i trigger vengono eseguiti in blocco, quindi nel caso di inserimenti di blocchi di righe, rifiutano tutte le righe e non solo quelle errate. Se si vuole eseguire un inserimento condizionato, si pu� usare una subquery in un trigger che obblighi il trigger a esaminare una riga alla volta.
Occorre predisporre una tabella di appoggio che contiene le righe da inserire, poi eseguire INSERT vendite FROM nuovevendite, che lancer� il trigger:

CREATE TRIGGER inserimento ON vendite FOR INSERT AS
IF (SELECT COUNT(*) FROM titoli, inserted
WHERE titoli.titolo = inserted.titolo) <> @@rowcount
BEGIN
	DELETE vendite FROM vendite, inserted
	WHERE vendite.titolo = inserted.titolo
	AND inserted.titolo NOT IN
		(SELECT titolo FROM titoli)
	PRINT 'Verranno aggiunte solo le righe che hanno un titolo corretto.'
END
Si deve tener presente l'ordine in cui vengono effettuate le operazioni: prima le righe vengono inserite nella tabella e in inserted, poi viene lanciato il trigger.


valori nulli impliciti ed espliciti
la clausola IF UPDATE(colonna) � vera in INSERT se alla colonna viene assegnato un valore.
Un valore NULL esplicito o default assegna un valore alla colonna, quindi attiva il trigger.
Un valore NULL implicito non assegna valore, quindi non attiva il trigger.

CREATE TABLE junk (a int NULL, b int NOT NULL)
go
CREATE TRIGGER junk ON JUNK FOR INSERT AS
IF UPDATE(a) AND UPDATE(b) PRINT 'lancio il trigger'
go
CREATE DEFAULT b_def AS 99
go
sp_bindefault b_def, junk.b
go
INSERT junk (a, b) VALUES (1, 2)	/* IF UPDATE vero */
INSERT junk VALUES (1, 2)		/* IF UPDATE vero */
INSERT junk VALUES (NULL, 2)		/* IF UPDATE vero (NULL esplicito)*/
INSERT junk (b) VALUES (2)		/* IF UPDATE falso */
INSERT junk (a) VALUES (2)		/* IF UPDATE vero (default su b)*/
gli stessi risultati sono prodotti anche da
IF UPDATE(a)
per creare un trigger che non permetta l'inserimento di valori NULL impliciti, si usa:
IF UPDATE(a) OR UPDATE(b)
dentro il trigger si pu� testare se a � NULL