Tag Archive: Sql Server


A really useful way to use an insert into is to create the destination table on the fly. Using this syntax, we can create a whole new table from a single insert statement, without creating the table first:

SELECT fname, lname
INTO users
FROM authors
GO

SQL Triggers

Um Trigger é um tipo de Procedure que é executado automaticamente após a alteração numa tabela ou view.

A sua utilização não é muito aconselhada, visto reduzir a perfomance da base de dados.

Sintaxe Base:

CREATE TRIGGER
nome_da_trigger
ON { TABLE | VIEW }
{
{ { FOR | AFTER | INSTEAD OF } { [ INSERT ] [ , ] [DELETE] } }
}

Exemplo:

CREATE TRIGGER

Calcula_Credito

On Conta_Corrente

AFTER INSERT,DELETE

AS

BEGIN

DECLARE @CLIENTE INT

SELECT @CLIENTE = Numero_Cliente FROM INSERTED

IF @CLIENTE IS NULL SELECT @CLIENTE = Numero_Cliente FROM DELETED

UPDATE Clientes

SET Clientes.Credito = (SELECT ISNULL(SUM(Conta_Corrente.valor), 0)

FROM Conta_Corrente

WHERE Conta_Corrente.Numero_Cliente = Clientes.Cliente)

END

Follow

Get every new post delivered to your Inbox.