Doubt - Insert SQL Server 2012 table

Asked

Viewed 74 times

2

I need to insert some information in the table called task, however, only when a certain condition is accepted that in the case of these two date fields, I put it for today for example if it was manual, ie I would insert in the system on today’s date, but I only want q to be inserted in the table when those dates are for example for the 20th of this 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'
)
  • you can use a Rigger to reject the inserts that do not respect the rules

  • and how it would be more or less this Trigger?

1 answer

0

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

Basic Example of Trigger

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

Even just receive the values on Trigger’s call and treat your rule.

Source: http://www.devmedia.com.br/triggers-no-sql-server-teoria-e-pratica-aplicada-em-uma-situacao-real/28194

Browser other questions tagged

You are not signed in. Login or sign up in order to post.