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

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

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

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

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 :: sql server datetime to string 
Sql :: sql server convert date to weekday 
Sql :: select 2 rows in sql 
Sql :: sql query to select records entered in last 24 hours 
Sql :: how to insert multiple rows in sql 
Sql :: mysql check if lowercase 
Sql :: oracle difference between two dates in years 
Sql :: Check database restore status sql script 
Sql :: mysql check date range 
Sql :: sum sqlalchemy 
Sql :: json extract 
Sql :: rename table column name in mysql 
Sql :: postgresql import a database of gzip 
Sql :: postgresql not case sensitive where in 
Sql :: dual in db2 
Sql :: mysql server not starting in xampp in mac 
Sql :: flask sqlalchemy update row 
Sql :: copy data from one table to another mysql 
Sql :: Write an SQL query to print details of the Workers who have joined in Feb’2014 
Sql :: orcale index size 
Sql :: installing mysql on centos 7 
Sql :: sql convert date format 
Sql :: postgresql float 2 decimal places 
Sql :: oracle nvl 
Sql :: sql alter table order by 
Sql :: SQL COUNT() With HAVING Clause 
Sql :: snowflake select from stage 
Sql :: sql select if two columns are equal 
Sql :: clear table sql 
Sql :: sql power function 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =