Doubt - Insert table SQL Server 2012

2

I need to insert some information in the table called task, however, only when a certain condition is accepted that in the case are these two date fields, I put it to today for example if it were manual, that is, it would insert into the system on the date of today, but I just want it to be inserted in the table when those dates are for example for the 20th of that month.

insert into Tarefa
(
    TarID,
    ProID,  
    TarData ,
    DataAberturaSistema 
)
values (
    168442,
    2,
    '2017-10-05 09:20:10.000',  
    '2017-10-05 09:20:10.015'
)
    
asked by anonymous 05.10.2017 / 14:25

1 answer

0

You will need to create a trigger, in the trigger body you will work the values entered by doing the validation:

Basic Trigger Example

CREATE TRIGGER TGR_VENDAS_AI
ON VENDAS
FOR INSERT
AS
BEGIN
    DECLARE
    @VALOR  DECIMAL(10,2),
    @DATA   DATETIME

SELECT @DATA = DATA, @VALOR = VALOR FROM INSERTED

UPDATE CAIXA SET SALDO_FINAL = SALDO_FINAL + @VALOR
WHERE DATA = @DATA

END
GO

Considering the tables:

CREATE TABLE CAIXA
(
    DATA            DATETIME,
    SALDO_INICIAL   DECIMAL(10,2),
    SALDO_FINAL     DECIMAL(10,2)
)
GO

INSERT INTO CAIXA
VALUES (CONVERT(DATETIME, CONVERT(VARCHAR, GETDATE(), 103)), 100, 100)
GO

CREATE TABLE VENDAS
(
    DATA    DATETIME,
    CODIGO  INT,
    VALOR   DECIMAL(10,2)
)
GO

You just have to receive the values in the trigger call and treat your rule.

Source: link

    
05.10.2017 / 15:32