早上好,
这是我在这里发表的第一篇文章,我一直在为我的工作申请一个APP的数据库,我需要一些关于触发器的线索,这让我很生气。
编辑:
鉴于下一个结构:
用户表:
[dbo].[ENGIE_Users]
(
[dbID] [int] IDENTITY(1,1) NOT NULL,
[Nombre] [nvarchar](100) NOT NULL,
[Apellidos] [nvarchar](200) NOT NULL,
[HashContraseña] [binary](64) NOT NULL,
[Email] [nvarchar](150) NOT NULL,
[Direccion] [nvarchar](150) NOT NULL,
[Codigo Postal] [nvarchar](5) NOT NULL,
[Telefono] [nvarchar](12) NOT NULL,
[Venta a Nº Cliente] [nvarchar](10) NOT NULL,
[POS Asignado] [nvarchar](10) NOT NULL,
[User_Number] AS ([dbo].[EngieUsers]([dbID])),
[Salt] [uniqueidentifier] NULL
约束: dbID主键 发送电子邮件No-Pk Unique
这是我们存储APP用户的地方。
用户资料表:
[dbo].[ENGIE_User_Material]
(
[ID User] [int] NOT NULL,
[EAN] [nvarchar](13) NOT NULL,
[Product Number] [nvarchar](20) NOT NULL,
[Venta] [tinyint] NOT NULL,
[Cantidad Actual] [int] NOT NULL,
[Cantidad Total] [int] NOT NULL,
[Descripcion Corta] [nvarchar](100) NOT NULL,
[PVP Neto] [decimal](38, 20) NOT NULL,
[% DTO] [decimal](38, 20) NOT NULL,
主键ID用户+产品编号+ EAN ID用户引用用户的dbID。
此表用于存储每个用户已归属于其“帐户”的产品数量。
订单表:
[dbo].[ENGIE_Ended_Orders]
(
[dbID] [int] IDENTITY(1,1) NOT NULL,
[ID User] [int] NOT NULL,
[Procesado] [tinyint] NOT NULL,
[Finalizado] [tinyint] NOT NULL,
[Fecha de realizacion] [date] NOT NULL,
[OT] [nvarchar](20) NOT NULL,
[Order Id] AS ([dbo].[EngiePedidos]([dbID])),
[Total Pedido] [decimal](38, 20) NULL,
主键dbID
ID用户外键引用用户的dbId 该表格将包含订单的“头部”。
订单内容表
[dbo].[ENGIE_Order_Content]
(
[Order Id] [int] NOT NULL,
[Product Number] [nchar](20) NOT NULL,
[Procesado] [tinyint] NOT NULL,
[Finalizado] [tinyint] NOT NULL,
[Fecha de realizacion] [date] NOT NULL,
[Cantidad] [int] NOT NULL,
[ID Pedido] [nchar](8) NULL,
主键产品编号+订单ID 外键订单ID引用订单dbID
所以,鉴于这种结构,我需要的是下一个限制:
在用户材料上: 它自己的实际数量永远不必克服总数量。 (插入/更新)
订单内容: 当插入或更新订单时,它必须满足上述限制(因此,如果您询问5,您必须有5个可用于此产品的单位才能购买) 此外,当它被插入/更新/删除时,触发器必须计算“订单总数”,如公式所示(数量*(价格 - (价格* 100 /折扣)))。
在订单上: 所以有两个表(与Orders / Order-Content结构相同,称为Ended Orders / Ended Order Con tent)这个表将在布尔[Finalizado] = 1时将这些表的所有行传输到另一个2。将恢复与此结束订单中的那些项目的用户相关联的实际数量。
完成的工作:
触发器: 在user-Material
上ALTER TRIGGER [dbo].[Qty_Restriction]
ON [ENGIE].[dbo].[ENGIE_User_Material]
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON;
IF(SELECT [Cantidad Actual] FROM INSERTED) > (SELECT [Cantidad Total] FROM INSERTED)
BEGIN
RAISERROR('La Cantidad Actual no debe superar la Cantidad Total',18,1);
END
ELSE BEGIN
INSERT INTO ENGIE.dbo.ENGIE_User_Material SELECT [ID User],[EAN], [Product Number],[Venta],[Cantidad Actual],[Cantidad Total],[Descripcion Corta], [PVP Neto],[% DTO] FROM INSERTED;
END
END
这不会让您插入实际数量多于总数量的注册表。
ALTER TRIGGER [dbo].[Qty_Restriction_2]
ON [ENGIE].[dbo].[ENGIE_User_Material]
INSTEAD OF UPDATE
AS
DECLARE @material varchar(13)
DECLARE @pno varchar(20)
DECLARE @usuario int
SET @material = (SELECT [EAN] FROM INSERTED)
SET @pno = (SELECT [Product Number] FROM INSERTED)
SET @usuario = (SELECT [ID User] FROM INSERTED)
BEGIN
SET NOCOUNT ON;
IF UPDATE([Cantidad Actual])
BEGIN
IF(SELECT [Cantidad Actual] FROM INSERTED) > (SELECT [Cantidad Total] FROM INSERTED)
BEGIN
RAISERROR('La Cantidad Actual no debe superar la Cantidad Total',18,1);
END
ELSE BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [Cantidad Actual] = (SELECT [Cantidad Actual] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
END
IF UPDATE([Cantidad Total])
BEGIN
IF(SELECT [Cantidad Actual] FROM INSERTED) > (SELECT [Cantidad Total] FROM INSERTED)
BEGIN
RAISERROR('La Cantidad Actual no debe superar la Cantidad Total',18,1);
END
ELSE BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [Cantidad Total] = (SELECT [Cantidad Total] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
END
IF UPDATE([Venta])
BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [Venta] = (SELECT [Venta] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
IF UPDATE([Descripcion Corta])
BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [Descripcion Corta] = (SELECT [Descripcion Corta] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
IF UPDATE([PVP Neto])
BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [PVP Neto] = (SELECT [PVP Neto] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
IF UPDATE([% DTO])
BEGIN
UPDATE [ENGIE].[dbo].[ENGIE_User_Material] SET [% DTO] = (SELECT [% DTO] FROM INSERTED) WHERE [Product Number] = @pno AND [EAN] = @material AND [ID User] = @usuario
END
END
这一个相同,但有更新。
在订单上:
CREATE TRIGGER [dbo].[TransferenceOrders_Inner]
ON [ENGIE].[dbo].[ENGIE_Orders]
AFTER UPDATE
AS
DECLARE @material nvarchar(20)
DECLARE @cant int
DECLARE @cantant int
DECLARE @usr int
DECLARE CURSOR_CANTIDAD CURSOR FOR
SELECT [Cantidad],[Product Number],[ID User] FROM [ENGIE].[dbo].[ENGIE_Order_Content] A, INSERTED I WHERE A.[Numero_Pedido] = I.Numero_Pedido
BEGIN
SET NOCOUNT ON;
IF UPDATE([Finalizado])
BEGIN
UPDATE A SET [Finalizado] = I.Finalizado FROM [ENGIE].[dbo].[ENGIE_Orders] A INNER JOIN INSERTED I ON A.dbID = I.dbID;
INSERT INTO [ENGIE].dbo.[ENGIE_Ended_Orders] ([Numero_Pedido],[ID User],[Fecha de realizacion],[OT],[ID Pedido],[Total Pedido])
(SELECT [dbID],[ID User],[Fecha de realizacion],[OT],[Numero_Pedido],[Total Pedido] FROM INSERTED)
INSERT INTO [ENGIE].dbo.[ENGIE_Ended_Order_Content] ([Numero_Pedido],[Product Number],[Fecha de realizacion],[Cantidad],[ID Pedido])
(SELECT [Numero_Pedido],[Product Number],[Fecha de realizacion],[Cantidad],[ID Pedido] FROM [ENGIE].[dbo].[ENGIE_Order_Content] A INNER JOIN INSERTED I ON A.Numero_Pedido = I.dbID)
OPEN CURSOR_CANTIDAD
FETCH CURSOR_CANTIDAD INTO @cant,@material,@usr
WHILE (@@FETCH_STATUS = 0)
BEGIN
UPDATE ENGIE.dbo.ENGIE_User_Material SET [Cantidad Actual] = (SELECT [Cantidad Actual] FROM ENGIE.dbo.ENGIE_User_Material A,INSERTED I WHERE A.[Product Number] = (SELECT [Product Number] FROM ENGIE.dbo.[ENGIE_Order_Content] B,INSERTED C WHERE B.Numero_Pedido = C.dbID)
AND A.[ID User] = I.[ID User])-@cant WHERE [Product Number] = @material AND [ID User] = @usr)
FETCH CURSOR_CANTIDAD INTO @cant,@material,@usr
END
CLOSE CURSOR_CANTIDAD;
DELETE A FROM [ENGIE].[dbo].[ENGIE_Orders] A INNER JOIN INSERTED I ON I.[dbID] = A.[dbID] AND A.[ID User] = I.[ID User];
END
DEALLOCATE CURSOR_CANTIDAD;
END
此触发器需要光标,因为它必须补充刚刚处理的订单内容的数量。
在Order_Content:
ALTER TRIGGER [dbo].[AutoIDOrder_DEL]
ON [ENGIE].[dbo].[ENGIE_Order_Content]
AFTER DELETE
AS
DECLARE @numped int
DECLARE @prod nchar(20)
DECLARE @pvp decimal(38,20)
DECLARE @pvpsdto decimal(38,20)
DECLARE @dto decimal(38,20)
DECLARE @cantidad int
SET @numped = (SELECT Numero_Pedido FROM INSERTED)
SET @prod = (SELECT [Product Number] FROM INSERTED)
SET @cantidad = (SELECT [Cantidad] FROM INSERTED)
SET @pvp = (SELECT [Total Pedido] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] = @numped)
BEGIN
SET NOCOUNT ON;
SET @pvpsdto = (SELECT [PVP Neto] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = @prod)
SET @dto = (SELECT [% DTO] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = @prod)
UPDATE [ENGIE].[dbo].[ENGIE_Orders] SET [Total Pedido] = @pvp-(@cantidad*((@pvpsdto*100)/@dto)) WHERE [dbID] = @numped
END
当您从订单内容中删除注册表时,此订单将从订单更新“订单总数”。
ALTER TRIGGER [dbo].[Qty_Restriction_3]
ON [ENGIE].[dbo].[ENGIE_Order_Content]
INSTEAD OF INSERT
AS
DECLARE @cantAnt int
DECLARE @cantAct int
DECLARE @cant int
DECLARE @pvpsdto decimal(38,20)
DECLARE @dto decimal(38,20)
DECLARE @total decimal(38,20)
SET @cant = (SELECT [Cantidad] FROM INSERTED)
SET @cantAnt = (SELECT [Cantidad Total] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @cantAct = (SELECT [Cantidad Actual] FROM [ENGIE].[dbo].[ENGIE_User_Material]WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @pvpsdto = (SELECT [PVP Neto] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @dto = (SELECT [% DTO] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @total = (SELECT [Total Pedido] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED))
IF(@total IS NULL)
BEGIN
SET @total = 0.0
END
BEGIN
SET NOCOUNT ON;
BEGIN
IF((@cantAct+@cant) > @cantAnt)
BEGIN
RAISERROR('La Cantidad Actual no debe superar la Cantidad Total',18,1);
END
ELSE BEGIN
INSERT INTO ENGIE.dbo.ENGIE_Order_Content SELECT * FROM INSERTED;
UPDATE [ENGIE].[dbo].[ENGIE_Order_Content] SET [ID Pedido] = (SELECT [Numero_Pedido] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] LIKE (SELECT [Numero_Pedido] FROM INSERTED)) WHERE [Numero_Pedido] LIKE (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number] LIKE (SELECT [Product Number] FROM INSERTED);
UPDATE [ENGIE].[dbo].[ENGIE_Order_Content] SET [Fecha de realizacion] = (SELECT [Fecha de realizacion] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED)) WHERE [Numero_Pedido] LIKE (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number] LIKE (SELECT [Product Number] FROM INSERTED);
----> AUQUI CALCULO DE TOTAL
UPDATE [ENGIE].[dbo].[ENGIE_Orders] SET [Total Pedido] = (@total+(@cant*(@pvpsdto-((@pvpsdto*@dto)/100)))) WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED)
END
END
END
这个将避免在不符合要求的订单内容上插入数量(实际数量< =总数量)+将根据您添加到订单的产品计算订单总数
ALTER TRIGGER [dbo].[Qty_Restriction_4]
ON [ENGIE].[dbo].[ENGIE_Order_Content]
INSTEAD OF UPDATE
AS
DECLARE @cantAnt int
DECLARE @cantAct int
DECLARE @cant int
DECLARE @pvpsdto decimal(38,20)
DECLARE @dto decimal(38,20)
DECLARE @bool bit
DECLARE @total decimal(38,20)
SET @bool = 0;
SET @cant = (SELECT [Cantidad] FROM INSERTED)
SET @userId = (SELECT [ID User] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED))
SET @cantAnt = (SELECT [Cantidad Total] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [ID User] = @userId AND [Product Number] = (SELECT [Product Number] FROM INSERTED))
SET @cantAct = (SELECT [Cantidad Actual] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [ID User] = @userId AND [Product Number] = (SELECT [Product Number] FROM INSERTED))
SET @pvpsdto = (SELECT [PVP Neto] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @dto = (SELECT [% DTO] FROM [ENGIE].[dbo].[ENGIE_User_Material] WHERE [Product Number] = (SELECT [Product Number] FROM INSERTED) AND [ID User] = (SELECT [ID User] FROM INSERTED))
SET @total = (SELECT [Total Pedido] FROM [ENGIE].[dbo].[ENGIE_Orders] WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED))
IF(@total IS NULL)
BEGIN
SET @total = 0.0
END
BEGIN
SET NOCOUNT ON;
IF UPDATE([Cantidad])
BEGIN
IF((@cantAct+@cant) > @cantAnt)
BEGIN
RAISERROR('La Cantidad Actual no debe superar la Cantidad Total',18,1);
--->ROLLBACK TRANSACTION;
END
ELSE BEGIN
UPDATE ENGIE.dbo.ENGIE_Order_Content SET [Cantidad] = (SELECT [Cantidad] FROM inserted) WHERE [Numero_Pedido]= (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number]= (SELECT [Product Number] FROM INSERTED)
SET @bool = 1;
END;
END;
IF UPDATE([Procesado])
BEGIN
UPDATE ENGIE.dbo.ENGIE_Order_Content SET [Procesado] = (SELECT [Procesado] FROM inserted) WHERE [Numero_Pedido]= (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number]= (SELECT [Product Number] FROM INSERTED)
END;
IF UPDATE([Finalizado])
BEGIN
UPDATE ENGIE.dbo.ENGIE_Order_Content SET [Finalizado] = (SELECT [Finalizado] FROM inserted) WHERE [Numero_Pedido]= (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number]= (SELECT [Product Number] FROM INSERTED)
END;
IF UPDATE([ID Pedido])
BEGIN
UPDATE ENGIE.dbo.ENGIE_Order_Content SET [ID Pedido] = (SELECT [ID Pedido] FROM inserted) WHERE [Numero_Pedido]= (SELECT [Numero_Pedido] FROM INSERTED) AND [Product Number]= (SELECT [Product Number] FROM INSERTED)
END;
---> AQUI METER LO DEL AFTER UPDATE, PARA QUE NO HAYA PROBLEMA, CON UN BOOLEAN
IF (@bool = 1)
BEGIN
SET NOCOUNT ON;
UPDATE [ENGIE].[dbo].[ENGIE_Orders] SET [Total Pedido] = (@total+(@cant*(@pvpsdto-((@pvpsdto*@dto)/100)))) WHERE [dbID] = (SELECT [Numero_Pedido] FROM INSERTED)
END;
END
这最后一个假装与插入相同,但在更新时,使实际数量的限制起作用并为[Order Total]列赋值
答案 0 :(得分:2)
您触发器不处理多行插入。如果
怎么办?SET @usuario = (SELECT [ID User] FROM INSERTED)
返回多个值。它会失败。并且触发器内的光标是非常糟糕的事情。您应该使用基于集合的方法重新编写触发器 你的前4个陈述应该重写
UPDATE A
SET [Finalizado] = i.Finalizado
FROM [ENGIE].[dbo].[ENGIE_Orders] A
INNER JOIN INSERTED I ON a.dbID = i.dbid;
INSERT INTO [ENGIE].dbo.[ENGIE_Ended_Orders] (
[Numero_Pedido]
,[ID User]
,[Fecha de realizacion]
,[OT]
,[ID Pedido]
)
SELECT [dbID]
,[ID User]
,[Fecha de realizacion]
,[OT]
,[Numero_Pedido]
FROM [ENGIE].[dbo].[ENGIE_Orders]
FROM inserted
INSERT INTO [ENGIE].dbo.[ENGIE_Ended_Order_Content] (
[Numero_Pedido]
,[Product Number]
,[Fecha de realizacion]
,[Cantidad]
,[ID Pedido]
)
SELECT [Numero_Pedido]
,[Product Number]
,[Fecha de realizacion]
,[Cantidad]
,[ID Pedido]
FROM [ENGIE].[dbo].[ENGIE_Order_Content] A
INNER JOIN INSERTED I ON A.Numero_Pedido = I.Dbid
DELETE A
FROM [ENGIE].[dbo].[ENGIE_Orders] A
INNER JOIN INSERTED I ON
WHERE I.[dbID] = A.DBID
AND A.[ID User] = I.USERID;
我不确定我是否符合您的标准(特别是连接请检查它)但触发器应该用基于设置的逻辑重写,我希望光标也可以被删除