CREATE TABLE [dbo].[AL2UDC] (
    [AL]          NVARCHAR (50) NOT NULL,
    [UDC]         NVARCHAR (50) NOT NULL,
    [DataCreaz]   DATETIME      NULL,
    [CodSoggetto] NCHAR (17)    NULL,
    CONSTRAINT [PK_AL2UDC] PRIMARY KEY CLUSTERED ([AL] ASC, [UDC] ASC),
    CONSTRAINT [FK_AL2UDC_ElencoAssiemi] FOREIGN KEY ([AL]) REFERENCES [dbo].[ElencoAssiemi] ([AL]) ON UPDATE CASCADE,
    CONSTRAINT [FK_AL2UDC_ElencoCartellini] FOREIGN KEY ([UDC]) REFERENCES [dbo].[ElencoCartellini] ([UDC]) ON UPDATE CASCADE
);




GO


-- =============================================
-- Author:	G.A.R	
-- Create date: 2014/03/19
-- Description:	trigger x inserimento valori in archivio storico alla cancellazione del dato
-- =============================================
CREATE TRIGGER [dbo].[trg_storicoAL2UDC] 
   ON  [dbo].[AL2UDC]
   AFTER UPDATE,DELETE
AS 
BEGIN

    IF (@@ROWCOUNT = 0) RETURN
	SET NOCOUNT ON;

    -- metto in storico i dati
	INSERT INTO StoricoAL2UDC(DataEv, AL, UDC, DataCreaz, CodSoggetto)
	(
		SELECT	GETDATE(), AL, UDC, DataCreaz, CodSoggetto
		FROM deleted
	)

END