Search
 
SCRIPT & CODE EXAMPLE
 

SQL

T-SQL Create Trigger

CREATE TRIGGER production.trg_product_audit
ON production.products
AFTER INSERT, DELETE
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO production.product_audits(
        product_id, 
        product_name,
        brand_id,
        category_id,
        model_year,
        list_price, 
        updated_at, 
        operation
    )
    SELECT
        i.product_id,
        product_name,
        brand_id,
        category_id,
        model_year,
        i.list_price,
        GETDATE(),
        'INS'
    FROM
        inserted i
    UNION ALL
    SELECT
        d.product_id,
        product_name,
        brand_id,
        category_id,
        model_year,
        d.list_price,
        GETDATE(),
        'DEL'
    FROM
        deleted d;
END
Code language: SQL (Structured Query Language) (sql)
Comment

how to set up a trigger in sql

Create Trigger Product_Details_tr
on Product_Details
for Insert
as
being
insert into Product_Details_Audit_Log(audit_ID, update_time_stamp)
select Id, CURRENT_TIMESTAMP
from inserted 
end
Comment

SQL Trigger

Syntax:
create trigger [trigger_name] 
[before | after]  
{insert | update | delete}  
on [table_name]  
[for each row]
as [print/select]
Example:
create trigger myDel
on tbl_pro
after delete
as
select * from tbl_product
print 'Record Deleted successfully!!!'
Comment

Trigger sql

CREATE DATABASE productdb;
GO
--create a table called Products
USE productdb;
CREATE TABLE Products
(
    Id INT IDENTITY PRIMARY KEY,
    ProductName NVARCHAR(30) NOT NULL,
    Manufacturer NVARCHAR(20) NOT NULL,
    ProductCount INT DEFAULT 0,
    Price MONEY NOT NULL
);

--create a trigger called Products_INSERT_UPDATE
CREATE TRIGGER Products_INSERT_UPDATE
ON Products
AFTER INSERT, UPDATE
AS
UPDATE Products
SET Price = Price + Price * 0.38
WHERE Id = (SELECT Id FROM inserted)

--one example
ISERT INTO Products(ProductName,Manufacturer,ProductCount,Price)
VALUES ('IPhone X',2,'Apple',68000)
SELECT * FROM Products
--answer will be Price = 9384000

--Turn off trigger
DISABLE TRIGGER Products_INSERT_UPDATE ON Products

--Turn on trigger
ENABLE TRIGGER Products_INSERT_UPDATE ON Products

--Delete trigger
DROP TRIGGER Products_INSERT_UPDATE
Comment

how use trigger in sql

CREATE TRIGGER Product_Details_tr 
BEFORE INSERT ON Product_Details 
FOR EACH ROW 
SET NEW.User_ID = CURRENT_USER();
Comment

Trigger Sql server


            
                
            
         CREATE TRIGGER production.trg_product_audit
ON production.products
AFTER INSERT, DELETE
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO production.product_audits(
        product_id, 
        product_name,
        brand_id,
        category_id,
        model_year,
        list_price, 
        updated_at, 
        operation
    )
    SELECT
        i.product_id,
        product_name,
        brand_id,
        category_id,
        model_year,
        i.list_price,
        GETDATE(),
        'INS'
    FROM
        inserted i
    UNION ALL
    SELECT
        d.product_id,
        product_name,
        brand_id,
        category_id,
        model_year,
        d.list_price,
        GETDATE(),
        'DEL'
    FROM
        deleted d;
END
Code language: SQL (Structured Query Language) (sql)
Comment

SQL trigger

CREATE OR REPLACE TRIGGER customers_audit_trg
    AFTER 
    UPDATE OR DELETE 
    ON customers
    FOR EACH ROW    
DECLARE
   l_transaction VARCHAR2(10);
BEGIN
   -- determine the transaction type
   l_transaction := CASE  
         WHEN UPDATING THEN 'UPDATE'
         WHEN DELETING THEN 'DELETE'
   END;

   -- insert a row into the audit table   
   INSERT INTO audits (table_name, transaction_name, by_user, transaction_date)
   VALUES('CUSTOMERS', l_transaction, USER, SYSDATE);
END;
/
Code language: SQL (Structured Query Language) (sql)
Comment

trigger sql

CREATE TRIGGER tên_trigger ON tên_bảng
FOR {DELETE, INSERT, UPDATE}
AS 
  câu_lệnh_sql
Comment

SQL trigger

CREATE TRIGGER [schema_name.]trigger_name
ON table_name
{FOR | AFTER | INSTEAD OF} {[INSERT] [,] [UPDATE] [,] [DELETE]}
AS
{sql_statements}
Comment

PREVIOUS NEXT
Code Example
Sql :: wp_query raw sql 
Sql :: PG::ForeignKeyViolation: ERROR: update or delete on table violates foreign key constraint 
Sql :: sql timezone 
Sql :: test database for sql 
Sql :: apex for oracle 11g 
Sql :: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , , = or when the subquery is used as an expression. 
Sql :: sql change primary key to composite key 
Sql :: incorrect datetime value sql table error 1292 
Sql :: sql strip non alphanumeric characters 
Sql :: mysql trigger to delete old data 
Sql :: SQL UNIQUE Constraint 
Sql :: Insert Multiple Rows at Once in SQL 
Sql :: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Client does not support authentication protocol requested by server; consider upgrading MySQL client 
Sql :: sql server concat null 
Sql :: like in sql 
Sql :: stuff in sql 
Sql :: migrations.RunSQL 
Sql :: SQL Greater Than Operator 
Sql :: postgresql comandos basicos 
Sql :: order by in sql 
Sql :: mariadb errno 121 
Sql :: swiftui onappear only once 
Sql :: like sql for second letter of the family name 
Sql :: mssql + bit + in python orm 
Sql :: formatting code with SQL Developer 
Sql :: mysql find char in string 
Sql :: user defined variables in sql 
Sql :: postgresql not in alternative 
Sql :: How to calculate average of average salary of departments? 
Sql :: show tables in oracle 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =