Time:09-27
CodePudding user response:
can trigger, only hint at this time or are not allowed to insert?
CREATE TRIGGER dbo. A_tri On [dbo]. BHome INSERT, UPDATE AS DECLARE @ Ph NVARCHAR (20); DECLARE @ sum1 DECIMAL DECLARE @ sum2 DECIMAL SELECT @ Ph=Inserted. PGH FROM Inserted The SET @ sum2=(SELECT SUM (cl) FROM Inserted) The SET @ sum2=@ sum2 + (SELECT SUM (CL) FROM B WHERE PGH=@ Ph) The SET @ sum1=(SELECT SUM (SL) FROM A WHERE PH=@ PH) IF @ sum2 & gt; @ sum1 The BEGIN RAISERROR (' number in 16, 8) END The ELSE IF the EXISTS (SELECT 1 FROM inserted) AND NOT the EXISTS (SELECT 1 FROM does) The BEGIN INSERT INTO the SELECT * FROM B Inserted END The ELSE IF the EXISTS (SELECT 1 FROM inserted) AND EXISTS (SELECT 1 FROM does) The BEGIN UPDATE SET CL=B Inserted. CL FROM Inserted WHERE p. GH=Inserted. The PGH END GO
create proc TTT @ ph varchar (40), @ newcl int as Declare @ oldphcl int, @ acl int Select @ oldphcl=sum (cl) from B where ph=@ ph Select @ acl=sum (cl) from a where ph=@ ph The set @ oldphcl=@ oldphcl + @ newcl If @ oldphcl> @ acl Raiserorrer (' number in 16, 8) The else Insert into b select @ newcl, @ ph
use stored procedure more suitable for calibration and execution of business logic, it is recommended to use stored procedure execution, create proc TTT @ ph varchar (40), @ newcl int as Declare @ oldphcl int, @ acl int Select @ oldphcl=sum (cl) from B where ph=@ ph Select @ acl=sum (cl) from a where ph=@ ph The set @ oldphcl=@ oldphcl + @ newcl If @ oldphcl> @ acl Raiserorrer (' number in 16, 8) The else Insert into b select @ newcl, @ ph
use less trigger, avoid future maintenance difficulties This can be used for a period of SQL to check When output is less than the orders is expected to produce
Quote: refer to the sixth floor wilson1966 response: Use less trigger, avoid future maintenance difficulties This can be used for a period of SQL to check When output is less than the orders is expected to produce Program already cannot be modified, just want to do a judgment in the process of data storage, could the implementation, please
Use less trigger, avoid future maintenance difficulties This can be used for a period of SQL to check When output is less than the orders is expected to produce
Page link:https//www.codepudding.com/database/36137.html