Created
September 28, 2012 01:43
-
-
Save alexsandro-xpt/3797518 to your computer and use it in GitHub Desktop.
Nostalgia: São Paulo/SP 26 de Agosto de 2004 | 03:50 da Matina
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE [moveis] | |
GO | |
/****** Object: Trigger [dbo].[organiza_registro] Date: 09/27/2012 22:50:44 ******/ | |
SET ANSI_NULLS ON | |
GO | |
SET QUOTED_IDENTIFIER ON | |
GO | |
/* | |
Autor: Alexsandro Souza Pereira | |
Data: São Paulo/SP 26 de Agosto de 2004 | 03:50 da Matina | |
Desc: | |
Tirei o COSTRAIN e fiz este codigo abaixo | |
BLOQUEIO_DE_REPETICAO_DE_CIDADES | |
id_cliente | |
id_cidade | |
id_estado | |
id_pais | |
*/ | |
ALTER TRIGGER [dbo].[organiza_registro] ON [dbo].[cad_area_atuacao_cliente] | |
FOR INSERT | |
AS | |
IF @@ROWCOUNT < 2 BEGIN | |
DECLARE @id_cidade INT, @id_estado INT, @id_pais INT, @id_cliente INT | |
SELECT @id_cidade = id_cidade, @id_estado = id_estado, @id_pais = id_pais, @id_cliente = id_cliente FROM inserted | |
IF(@id_cidade IS NOT NULL) BEGIN | |
DELETE cad_area_atuacao_cliente WHERE id_cliente = @id_cliente AND id_cidade = @id_cidade AND id_estado IS NOT NULL AND | |
(((SELECT COUNT(*) FROM cad_area_atuacao_cliente b WHERE b.id_cliente = @id_cliente AND b.id_estado IS NULL AND b.id_pais = @id_pais) = 1) | |
OR ((SELECT COUNT(*) FROM cad_area_atuacao_cliente b WHERE b.id_cliente = @id_cliente AND b.id_estado = @id_estado AND b.id_pais = @id_pais AND b.id_cidade IS NULL) = 1)) | |
END ELSE IF(@id_cidade IS NULL AND @id_estado IS NOT NULL) BEGIN | |
DELETE cad_area_atuacao_cliente WHERE id_cliente = @id_cliente AND id_estado = @id_estado AND ((id_cidade IS NOT NULL) | |
OR ((SELECT COUNT(*) FROM cad_area_atuacao_cliente b WHERE b.id_cliente = @id_cliente AND b.id_estado IS NULL AND b.id_pais = @id_pais) = 1) ) | |
END ELSE IF(@id_estado IS NULL AND @id_pais IS NOT NULL) BEGIN | |
DELETE cad_area_atuacao_cliente WHERE id_cliente = @id_cliente AND id_estado IS NOT NULL AND id_pais = @id_pais | |
END | |
END ELSE BEGIN | |
RAISERROR ('Não é possivel usar INSERT INTO em massa por causa do tratamento da Trigger!',16,1) | |
ROLLBACK | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment