Home > database >  Excuse me this kind of situation the trigger can be solved
Excuse me this kind of situation the trigger can be solved

Time:09-27

CodePudding user response:

Can be used to trigger, at that time only hint or not allowed to insert?

CodePudding user response:

reference 1/f, February 16 response:
can trigger, only hint at this time or are not allowed to insert?

Insert or update is not allowed in this time, can we write about this statement, I know nothing to trigger, thank you

CodePudding user response:

This is for each insert a sample number
the condition of the
 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

CodePudding user response:

Use the 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



CodePudding user response:

to learn

CodePudding user response:

Use less trigger, avoid future maintenance difficulties
This can be used for a period of SQL to check
When the expected output is less than the orders to production

CodePudding user response:

reference 4 floor netcup response:
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




Program already cannot be modified, just want to do a judgment in the process of data storage, this can be achieved, please

CodePudding user response:

refer to 6th 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, this can be achieved, please

CodePudding user response:

refer to the eighth floor lqbr123 response:
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


Of course I can

CodePudding user response:

As long as you can get the input value can use stored procedures,

CodePudding user response:

If the check from the conditions of the judgment associated other tables, can only use the stored procedure, if check condition in judging conditions of different fields in a table, with constraints can also,
  • Related