EULANDA® ERP System Head Start thru Technology
Trigger represent an elegant method, own functions and also changes at expirations, to integrate in the EULANDA® ERP Software.
Trigger are special stored procedures. They can contain arbitrary SQL commands and they are called automatically with certain database operations like (paste, change, delete of data records) by the SQL server.
The possibility to spend error messages in the trigger, which the final user gets indicated and the possibility to rollback the original data base operations those the trigger released. That opens a far spectrum of application possibilities.
Some to the possibility gave up for a trigger are:
Trigger are assigned individual tables. And to each trigger fixed with which operations it to be triggered. These operations are:
INSERT, UPDATE und DELETE
The individual trigger can at the same time supervise also several of these operations. But the same trigger cannot be deposited with several tables, even if the used columns equal loud.
EULANDA® defines already to nearly all tables own trigger, which illustrate the business logic. Microsoft SQL-Server® has the pleasant characteristic as many as desired triggers per table to deposit. You as developers are thus in this regard none border in the expandability set.
So that user triggers smoothly integrate themselves into the EULANDA® and not to malfunctions lead you have to consider some rules.
Example of a user trigger:
CREATE TRIGGER TR_USER_AF_INS_StatusPruefung ON Auftrag
FOR INSERT, UPDATE
AS
/* This must be the first command in a trigger */
SET NOCOUNT ON
/* Here begin the actual trigger commands */
Up to very few exceptions you would only like a reduced number of columns to supervise in a trigger. Transact-SQL supplies with the function UPDATE (column name) the possibility to determine the changed columns. You have always to fit out the triggers commands in a condition block, which contains all desired fields:
IF UPDATE (Status) OR UPDATE (Object) OR UPDATE (Date) BEGIN /* Your trigger commands */ END
Hint:
The UPDATE function in DELETE trigger returns always false.
Minimize and/or optimize the SELECT or INSERT commands. If it is necessary you put on indices in the appropriate tables, in order to shorten the run time.
Only because the function UPDATE (column name) supplies true, must the column not been changed. e.g. the command change:
UPDATE Address SET RabattGr = 'A' WHERE RabattGr = 'A'
not the group of discounts but UPDATE responds nevertheless. If a complex SQL command of a "genuine" change column depends, you should make further examinations. The following command produces for eample a temporary table with the address IDs which were really changed:
DECLARE @t TABLE (id int)
INSERT @t SELECT i.id
FROM inserted i, deleted d
WHERE i.id = d.id AND i.RabattGr <> d.RabattGr
In this example with changing the order position, it will be examined if a positive yield is present. If that is not the case, an error message is spent and the changes are rejected. (Hint: this examination could be accomplished also with an CHECK CONSTRAINT ).
CREATE TRIGGER TR_USER_AFP_INSUPD_Ertrag ON AuftragPos
FOR INSERT, UPDATE
AS
SET NOCOUNT ON
IF ( UPDATE(VkRab) OR UPDATE(Menge) OR UPDATE(PreisEH) )
BEGIN
IF EXISTS (SELECT * FROM inserted
WHERE (Menge > 0) AND (Ertrag < 0) ) BEGIN
RAISERROR('[VENDOR:USER][ADRESS:USER]The profit is less!', 16,1)
ROLLBACK
END
END
This trigger protocols changes of the date and/or order status of the order. Here only orders considered those are older than one hour. The information is held in the SQL server protocol. This can be seen in the SQL enterprise manager or in the Windows explorer as text file.
CREATE TRIGGER TR_USER_AF_UPD_Datum ON Auftrag
FOR UPDATE
AS
SET NOCOUNT ON
IF UPDATE(Date) OR UPDATE(OrderStatus)
BEGIN
/* Examine if orders exist, which are older as one hour */
IF EXISTS(SELECT * FROM inserted
WHERE DATEDIFF(hour,CreateDate,GETDATE())>1 )
BEGIN
/* Declaration of the used variables */
DECLARE @userid int, @nr int, @count int
/* Determine the order number and the number of orders.
** If several orders are concerned Falls
** in this example only the smallest number is determined. */
SELECT @nr = MIN(KopfNummer), @count = COUNT(*) FROM inserted
/* Creates an entry in the table cnProesses
** with all indications of the user and workstation
** at that the instruction will be implemented */
EXEC cn_UserId @userid OUT
/* Entry of an error message into SQL server protocol
** The severity level is 1. thus the error is not passed on at EULANDA */
RAISERROR('Datum/BestellStatus in %d Auftragsposition(-en)
changed (Auftrag #%d)
´from process %d (look table cnProcesses for more details)',
1,1, @count, @nr, @userid) WITH LOG
END
END
The following SQL script shows that an UPDATE command also must not change lines. That means for the trigger programmer, that they have to consider also the case of "null" lines in the pseudo tables:
CREATE TRIGGER TR_USER_AR_UPD_CountTest ON Article
FOR UPDATE
AS
/*
** The trigger serves ONLY for the elucidation the problem described in the text.
** This is otherwise NO valid trigger for the use
** with the EULANDA® ware economic !
*/
IF UPDATE(Barcode)
SELECT COUNT(*) [Concerned lines with barcode-change]
FROM inserted
ELSE SELECT COUNT(*) [No barcode-change] FROM inserted
GO
UPDATE Artikel SET Barcode = '4711' WHERE 1 = 2
GO
DROP TRIGGER TR_USER_AR_UPD_CountTest
Name convention
cn_UserId
RAISERROR
Creating of User indices
Creating of User-Check-Constraints