“Déclencheur SQL” Réponses codées

Création d'Un déclenchent SQL

 CREATE OR REPLACE TRIGGER trigg_example
 BEFORE INSERT OR UPDATE ON table_example
 FOR EACH ROW
 WHEN (new.no_line > 0)
 DECLARE
     evol_exemple number;
 BEGIN
     evol_exemple := :new.exemple  - :old.exemple;
     DBMS_OUTPUT.PUT_LINE('  evolution : ' || evol_exemple);
 END;
Breakable Barracuda

Déclencher 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
Info Important

Déclencheur SQL

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!!!'
Maaz

Déclencher le serveur SQL


            
                
            
         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)
Fragile Fox

déclencher le stock de vrifier

 
BEGIN
 
UPDATE `product` 
    SET `price` =  NEW.price
    , `quantity` = NEW.quantity 
    , date_modified = NOW() 
    WHERE `product_id` = NEW.product_id;
    AND price > NEW.price 
    AND quantity > 0
;
 
END
Breakable Barracuda

Réponses similaires à “Déclencheur SQL”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code