Topic: Trigger

any one can tell about trigger and how it is created?

Re: Trigger

unforgettable wrote:

any one can tell about trigger and how it is created?

procedure triggerCreate;
begin
    SQLExecute(
    'CREATE TRIGGER IF NOT EXISTS my_log_insert AFTER UPDATE ON request '+
    'BEGIN '+
    '    INSERT INTO statistic(data, status) VALUES (datetime(''now'', ''localtime''),''New''); '+
    'END;'
    );
end;

procedure triggerDelete;
begin
SQLExecute('DROP TRIGGER IF EXISTS  my_log_insert');
end;

Re: Trigger

Thank you sibprogsistem. Would you like to explain this script? Especially ( '    INSERT INTO statistic(data, status) VALUES (datetime(''now'', ''localtime''),''New''); '+)

Re: Trigger

unforgettable wrote:

Thank you sibprogsistem. Would you like to explain this script? Especially ( '    INSERT INTO statistic(data, status) VALUES (datetime(''now'', ''localtime''),''New''); '+)

 
 
INSERT INTO - create a new entry
Table_Name(field_1, field_1) VALUES(vlue_1, value_2)

  field_1 = value_1 - the value is written to the field

Re: Trigger

Thank you again sibprogsistem. Some question about trigger. what is trigger and why is it created. Any page that help me  to understand trigger.
"my_log_insert" is trigger name or table name?

Re: Trigger

unforgettable wrote:

Thank you again sibprogsistem. Some question about trigger. what is trigger and why is it created. Any page that help me  to understand trigger.
"my_log_insert" is trigger name or table name?

A trigger is a special kind of stored procedure in a database. The peculiarity of triggers is that the SQL code written in the body of the trigger will be executed after an event occurs in the database. UPDATE, INSERT, DELETE and SELECT.

"my_log_insert"  -  is trigger name

AFTER UPDATE ON request  -- after update in table (request)

Re: Trigger

Thank you sibprogsistem. Next I want to know where trigger will be created. For example I have a project and a button is used to display records on  a form1 and tablegrid1 and procedure is click button. After line of  (procedure Form1_Button1_OnClick (Sender: TObject);
begin)    --------- so here we enter script for trigger?

Re: Trigger

unforgettable wrote:

Thank you sibprogsistem. Next I want to know where trigger will be created. For example I have a project and a button is used to display records on  a form1 and tablegrid1 and procedure is click button. After line of  (procedure Form1_Button1_OnClick (Sender: TObject);
begin)    --------- so here we enter script for trigger?


You can write triggers in any part.
Triggers are written to the database and fire automatically depending on how you program them.
 
triggers can be written between

begin
     there is a trigger
end

or at the moment of launching the form itself

procedure Form1_OnShow(Sender: TObject; Action: string);
begin
     there is a trigger
end;

you can also create triggers on button clicks.

9 (edited by unforgettable 2022-02-21 08:51:21)

Re: Trigger

Hi sibprogsistem,
                          how are you? Can you attached any project with trigger that will make me understand about trigger? because your script display error message.