501 lines
12 KiB
Transact-SQL
501 lines
12 KiB
Transact-SQL
-- STEP 1: struttura tabelle + INDICI, TRIGGERS...
|
|
|
|
create table AnagAzioniUtente(
|
|
codAzione nvarchar(10) not null constraint PK_AnagAzioniUtente primary key,
|
|
descrAzione nvarchar(50)
|
|
);
|
|
go
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
drop table AS400_BatchMovimenti;
|
|
go
|
|
|
|
create table AS400_BatchMovimenti(
|
|
IdxBatch int not null identity constraint PK_AS400_BatchMovimenti primary key,
|
|
DataIns datetime not null,
|
|
numOp int,
|
|
CodEvento nvarchar(10) not null,
|
|
Particolare nvarchar(15) not null,
|
|
UDC nvarchar(50) not null,
|
|
CodMagAS nvarchar(50) not null,
|
|
Quantita decimal(10,2) not null,
|
|
DataBatch datetime,
|
|
DataVerifica datetime
|
|
);
|
|
go
|
|
|
|
alter table AS400_BatchMovimenti add
|
|
constraint FK_AS400_BatchMovimenti_AS400_MagCont foreign key(CodMagAS) references AS400_MagCont(CodMagAS) on update cascade,
|
|
constraint FK_AS400_BatchMovimenti_AnagTipoEvento foreign key(CodEvento) references AnagTipoEvento(CodEvento) on update cascade;
|
|
go
|
|
|
|
create index IX_AS400_BatchMovimenti_DataBatch_Filter on AS400_BatchMovimenti(DataBatch)
|
|
where ([DataBatch] IS NULL);
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'dataora inserimento in coda caricamento', 'SCHEMA', 'dbo', 'TABLE', 'AS400_BatchMovimenti', 'COLUMN', 'DataIns';
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'NON MODIFICARE: nel codice si usano questi campi x chiamate...', 'SCHEMA', 'dbo', 'TABLE', 'AS400_BatchMovimenti', 'COLUMN', 'CodEvento';
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'dataora di caricamento batch verso AS', 'SCHEMA', 'dbo', 'TABLE', 'AS400_BatchMovimenti', 'COLUMN', 'DataBatch';
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', N'dataora in cui è stato verificato il caricamento su AS', 'SCHEMA', 'dbo', 'TABLE', 'AS400_BatchMovimenti', 'COLUMN', 'DataVerifica';
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
drop index i_IdxPosizione on ElencoCartellini;
|
|
go
|
|
|
|
create index i_IdxPosizione on ElencoCartellini(IdxPosizione,CodCS)
|
|
include(Particolare);
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table ElencoListePrelievo add
|
|
DataModStato datetime constraint DataModStato_default default (getdate());
|
|
go
|
|
|
|
create index IX_ElencoListePrelievo_DataCreaz on ElencoListePrelievo(DataCreaz);
|
|
go
|
|
|
|
create index ix_ElencoListePrelievo_CodTipoLista_CodSoggettoCodStatoLista on ElencoListePrelievo(CodTipoLista,CodSoggetto,CodStatoLista)
|
|
include(CodLista);
|
|
go
|
|
|
|
update ElencoListePrelievo set DataModStato=(getdate());
|
|
go
|
|
|
|
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table ElencoListePrelievo_storico add
|
|
DataModStato datetime;
|
|
go
|
|
|
|
drop index ix_ElencoListePrelievo_storico_CodStatoLista_Filtered on ElencoListePrelievo_storico;
|
|
go
|
|
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
-- =============================================
|
|
-- Author:
|
|
-- Create date: 2011-06-21
|
|
-- Description: trigger x storico in tab
|
|
-- storico LDP completata e Reset LDP Scaricata
|
|
-- Mod. Date : 2012-10-17
|
|
-- =============================================
|
|
alter TRIGGER trg_ListePrelievoUpd
|
|
ON ElencoListePrelievo
|
|
AFTER UPDATE
|
|
AS
|
|
BEGIN
|
|
|
|
-- Se nessun aggiornamento esco ( DEVE essere il primo comando )
|
|
IF (@@ROWCOUNT = 0) RETURN
|
|
|
|
-- setup variabili e verbosità
|
|
SET NOCOUNT ON;
|
|
DECLARE @dataOra AS DATETIME = GETDATE()
|
|
|
|
IF( UPDATE(CodStatoLista))
|
|
BEGIN
|
|
INSERT INTO ElencoListePrelievo_storico
|
|
(DataOraDelete,CodLista,CodTipoLista,CodCS,DataCreaz,CodCliente,RagSociale,CodSoggetto,Particolare,DescParticolare,DisegnoGrezzo,
|
|
Esponente,Figura,CodImballo,QtaTot,CodStatoLista,Destinatario,DestinatarioDescrizione, DataModStato)
|
|
SELECT @dataOra, d.CodLista,d.CodTipoLista,d.CodCS,d.DataCreaz,d.CodCliente,d.RagSociale,d.CodSoggetto,d.Particolare,d.DescParticolare,d.DisegnoGrezzo,
|
|
d.Esponente,d.Figura,d.CodImballo,d.QtaTot,d.CodStatoLista,d.Destinatario,d.DestinatarioDescrizione, d.DataModStato
|
|
FROM deleted AS d
|
|
INNER JOIN inserted AS i ON d.CodLista = i.CodLista
|
|
WHERE ( d.CodStatoLista <> i.CodStatoLista ) -- 99 solo se lo stato lista è cambiato
|
|
AND NOT ( ( d.CodStatoLista = 0 AND i.CodStatoLista = 1 AND d.CodTipoLista = '00-Smart' ) -- escludo la storicizzazione dei primi due stati
|
|
OR ( d.CodStatoLista = 1 AND i.CodStatoLista = 2 AND d.CodTipoLista = '00-Smart' ) ) -- delle smart list che sono creati contemporaneamente
|
|
|
|
--WHERE ( d.CodStatoLista = 3 AND i.CodStatoLista = 4 ) -- Storicizzo chi ha completato la Lista
|
|
-- OR ( d.CodStatoLista = 4 AND i.CodStatoLista < 4 ) -- Storicizzo se la lista Scaricata è viene resettata
|
|
-- OR ( d.CodStatoLista = 3 AND i.CodStatoLista < 3 ) -- Storicizzo se la lista Completata è viene resettata
|
|
-- OR ( d.CodStatoLista = 0 AND i.CodStatoLista > 0 ) -- Storicizzo lo stato Bozza
|
|
-- OR ( d.CodStatoLista = 1 AND i.CodStatoLista > 1 ) -- Storicizzo lo stato Generata
|
|
-- OR ( d.CodStatoLista = 2 AND i.CodStatoLista > 2 ) -- Storicizzo lo stato In Carico
|
|
END
|
|
END
|
|
go
|
|
|
|
-- =============================================
|
|
-- Author: Samuele E. Locatelli
|
|
-- Create date: 2011-05-20
|
|
-- Description: trigger x inserimento in tab
|
|
-- storico record cancellati
|
|
-- Mod. Date : 2012-10-17
|
|
-- =============================================
|
|
alter TRIGGER trg_ListePrelievoDel
|
|
ON ElencoListePrelievo
|
|
AFTER DELETE
|
|
AS
|
|
BEGIN
|
|
|
|
-- Se nessun aggiornamento esco ( DEVE essere il primo comando )
|
|
IF (@@ROWCOUNT = 0) RETURN
|
|
|
|
-- setup variabili e verbosità
|
|
SET NOCOUNT ON;
|
|
-- inserisco in storico
|
|
INSERT INTO ElencoListePrelievo_storico
|
|
(DataOraDelete,CodLista,CodTipoLista,CodCS,DataCreaz,CodCliente,RagSociale,CodSoggetto,Particolare,DescParticolare,DisegnoGrezzo,
|
|
Esponente,Figura,CodImballo,QtaTot,CodStatoLista,Destinatario,DestinatarioDescrizione, DataModStato)
|
|
|
|
SELECT GETDATE(), CodLista,CodTipoLista,CodCS,DataCreaz,CodCliente,RagSociale,CodSoggetto,Particolare,DescParticolare,DisegnoGrezzo,
|
|
Esponente,Figura,CodImballo,QtaTot,CodStatoLista,Destinatario,DestinatarioDescrizione, DataModStato
|
|
FROM deleted
|
|
|
|
|
|
END
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table ElencoPostazioni add
|
|
note nvarchar(250);
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
/***************************************
|
|
* STORED stp_EP_update
|
|
*
|
|
* update di una postazione + stampante
|
|
|
|
* Steamware, S.E.L.
|
|
* mod: 2012.10.02
|
|
*
|
|
****************************************/
|
|
create PROCEDURE stp_EP_update
|
|
(
|
|
@Original_codPostazione NVARCHAR(250),
|
|
@codPostazione NVARCHAR(250),
|
|
@stampante NVARCHAR(250),
|
|
@note NVARCHAR(250)
|
|
)
|
|
AS
|
|
|
|
UPDATE ElencoPostazioni
|
|
SET codPostazione = @codPostazione,
|
|
stampante = @stampante,
|
|
note = @note
|
|
WHERE codPostazione = @Original_codPostazione
|
|
|
|
RETURN
|
|
go
|
|
|
|
/***************************************
|
|
* STORED stp_EP_delete
|
|
*
|
|
* elimina una postazione + stampante
|
|
|
|
* Steamware, S.E.L.
|
|
* mod: 2012.10.02
|
|
*
|
|
****************************************/
|
|
create PROCEDURE stp_EP_delete
|
|
(
|
|
@Original_codPostazione NVARCHAR(250)
|
|
)
|
|
AS
|
|
|
|
DELETE
|
|
FROM ElencoPostazioni
|
|
WHERE codPostazione = @Original_codPostazione
|
|
|
|
RETURN
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table PosizioneUdcCorrente add
|
|
CodSoggetto nchar(17);
|
|
go
|
|
|
|
create index IX_PosizioneUdcCorrente_idxCella on PosizioneUdcCorrente(IdxCella)
|
|
include(DataRif);
|
|
go
|
|
|
|
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
alter table PosizioneUdcStorico add
|
|
CodSoggetto nchar(17);
|
|
go
|
|
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
set ANSI_NULLS on;
|
|
go
|
|
|
|
-- =============================================
|
|
-- Author: Samuele E. Locatelli
|
|
-- Create date: 2009-09-13
|
|
-- Mod date: 2012-09-17
|
|
-- Description: trigger x inserimento valori in archivio storico alla cancellazione/modifica del dato (tranne curr user)
|
|
-- =============================================
|
|
alter TRIGGER trg_storico
|
|
ON PosizioneUdcCorrente
|
|
AFTER DELETE,UPDATE
|
|
AS
|
|
BEGIN
|
|
-- SET NOCOUNT ON added to prevent extra result sets from
|
|
-- interfering with SELECT statements.
|
|
SET NOCOUNT ON;
|
|
|
|
DECLARE @FlaStoricizza AS Bit = 0; -- impostazione x test storicizza sempre attivo
|
|
|
|
-- Se è una cancellazione Storicizzo
|
|
IF ( SELECT COUNT(*) FROM inserted ) = 0
|
|
SET @FlaStoricizza = 1 ;
|
|
|
|
-- devo EVITARE di salvare se c'è solo modifica del CodSoggetto...
|
|
IF( UPDATE(UDC) OR UPDATE(IdxCella) OR UPDATE(CodCS) OR UPDATE(DataRif) )
|
|
SET @FlaStoricizza = 1;
|
|
|
|
|
|
-- IF( UPDATE(UDC) OR UPDATE(IdxCella) OR UPDATE(CodCS) OR UPDATE(DataRif) )
|
|
|
|
IF @FlaStoricizza = 1
|
|
BEGIN
|
|
-- metto in storico la posizione attuale dell'UDC (SE esiste) CANCELLATO
|
|
INSERT INTO PosizioneUdcStorico(UDC, IdxCella, CodCS, DataInizio, DataFine, CodSoggetto)
|
|
(
|
|
SELECT UDC, IdxCella, CodCS, DataRif as DataInizio, GETDATE() as DataFine, CodSoggetto
|
|
FROM deleted
|
|
)
|
|
END
|
|
END
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
drop index IX_RigheListePrelievo_UDC_Prelevato on RigheListePrelievo;
|
|
go
|
|
|
|
create index ix_RigheListePrelievo_UDC_Prelevato on RigheListePrelievo(UDC,Prelevato)
|
|
include(Qta);
|
|
go
|
|
|
|
create index IX_RigheListePrelievo_NON_Prelevate on RigheListePrelievo(Prelevato)
|
|
include(CodLista,Qta)
|
|
where ([PRELEVATO]=(0));
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
drop index i_dataRap on RilPro.RapQual;
|
|
go
|
|
|
|
create index i_dataRap on RilPro.RapQual(DataRapQual)
|
|
include(BenesQual,CodFor,CodLega,nRapQual,Qta,UDC);
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
create table StoricoAzioniOperatore(
|
|
idxEvento int not null identity constraint PK_StoricoAzioniOperatore primary key,
|
|
DataOra datetime not null,
|
|
CodSoggetto nchar(17) not null,
|
|
codPostazione nvarchar(250),
|
|
clientIP nvarchar(250),
|
|
UDC nvarchar(50),
|
|
Particolare nvarchar(15),
|
|
codAzione nvarchar(10),
|
|
descrizione nvarchar(500)
|
|
);
|
|
go
|
|
|
|
alter table StoricoAzioniOperatore add
|
|
constraint FK_StoricoAzioniOperatore_AnagAzioniUtente foreign key(codAzione) references AnagAzioniUtente(codAzione) on update cascade;
|
|
go
|
|
|
|
create index ix_dataOra on StoricoAzioniOperatore(DataOra);
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
set xact_abort on;
|
|
go
|
|
|
|
begin transaction;
|
|
go
|
|
|
|
exec sp_rename 'PK_StoricoEventi', 'tmp__PK_StoricoEventi', 'OBJECT';
|
|
go
|
|
|
|
exec sp_rename 'StoricoCartellini', 'tmp__StoricoCartellini_0', 'OBJECT';
|
|
go
|
|
|
|
create table StoricoCartellini(
|
|
IdxEv int not null identity constraint PK_StoricoEventi primary key,
|
|
DataEv datetime not null,
|
|
CodEvento nvarchar(10),
|
|
ModDate datetime,
|
|
UDC nvarchar(50) not null,
|
|
CodCS nchar(2),
|
|
CodCliente nvarchar(6),
|
|
RagSociale nvarchar(35),
|
|
Particolare nvarchar(15),
|
|
DescParticolare nvarchar(30),
|
|
DisegnoGrezzo nvarchar(30),
|
|
Esponente nvarchar(6),
|
|
CodImpianto nvarchar(50),
|
|
DescImpianto nvarchar(50),
|
|
CodStampo nvarchar(8),
|
|
Figura nvarchar(4),
|
|
DataFus datetime,
|
|
TurnoFus int,
|
|
CodImballo nvarchar(15),
|
|
CodSoggetto nchar(17),
|
|
NumCont int,
|
|
Tara float,
|
|
Qta decimal(10,2),
|
|
CodStato nvarchar(50),
|
|
IdxPosizione int,
|
|
PesoTot float,
|
|
PesoCad float,
|
|
Note nvarchar(500)
|
|
);
|
|
go
|
|
|
|
exec sp_addextendedproperty 'MS_Description', 'NON MODIFICARE: nel codice si usano questi campi x chiamate...', 'SCHEMA', 'dbo', 'TABLE', 'StoricoCartellini', 'COLUMN', 'CodEvento';
|
|
go
|
|
|
|
set identity_insert StoricoCartellini on;
|
|
go
|
|
|
|
insert into StoricoCartellini(IdxEv,DataEv,CodEvento,ModDate,UDC,CodCS,CodCliente,RagSociale,Particolare,DescParticolare,DisegnoGrezzo,Esponente,CodImpianto,DescImpianto,CodStampo,Figura,DataFus,TurnoFus,CodImballo,CodSoggetto,NumCont,Tara,Qta,CodStato,IdxPosizione,PesoTot,PesoCad,Note) select IdxEv,DataEv,CodEvento,ModDate,UDC,CodCS,CodCliente,RagSociale,Particolare,DescParticolare,DisegnoGrezzo,Esponente,CodImpianto,DescImpianto,CodStampo,Figura,DataFus,TurnoFus,CodImballo,CodSoggetto,NumCont,Tara,Qta,CodStato,IdxPosizione,PesoTot,PesoCad,Note from tmp__StoricoCartellini_0;
|
|
go
|
|
|
|
set identity_insert StoricoCartellini off;
|
|
go
|
|
|
|
drop table tmp__StoricoCartellini_0;
|
|
go
|
|
|
|
commit;
|
|
go
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- registro versione...
|
|
INSERT INTO [dbo].[LogUpdateDb] ([Versione],[Data]) VALUES(500, GETDATE())
|
|
GO
|