Trigger doesn't work at all - mariadb

I have these tables: Attendence(id,start_time as TIME,end_time as TIME) and editattendencerequest(id,attendence_id,new_start_time,new_end_time,status as ENUM('Approved','Rejected','Pending')
Now when an edit request is approved by updating its status I want the database to automatically update the specified attendence's start_time and end_time to new_start_time and new_end_time.
I defined this triger through phpmyadmin:
And here is the trigger in code :
IF NEW.status <> OLD.status THEN
IF NEW.status = 'Approved' THEN
UPDATE attendence set start_time = NEW.start_time and end_time=NEW.end_time where id = NEW.attendence_id;
END IF;
END IF
Yet when I insert an attendence, insert an edit request and then update its status to Approved all through phpmyadmin I don't see the attendence updated at all.
So why isn't it working ? and how to make it work ?
My OS is windows 7 and using xampp 7.1.11.

The and word in the SET portion of the UPDATE is the issue. The fields in the SET portion should be separated by commas, try this instead:
IF NEW.status <> OLD.status THEN
IF NEW.status = 'Approved' THEN
UPDATE attendence
SET start_time = NEW.start_time, end_time = NEW.end_time
WHERE id = NEW.attendence_id;
END IF;
END IF

Related

Trouble with a cursor loop in an SQL event

My current code:
CREATE EVENT lifeinsurance_reset
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP()
ON COMPLETION NOT PRESERVE
ENABLE
BEGIN
DECLARE Cur = CURSOR FOR
SELECT `char_id` FROM `life_insurance` WHERE `renewal_date` <= CURRENT_TIMESTAMP()
OPEN Cur
LOOP
UPDATE `characters` SET `bank` = `bank` - 25000 WHERE `id` = Cur.char_id;
UPDATE `life_insurance` SET `renewal_date` = DATE_ADD(NOW(), INTERVAL 1 MONTH) WHERE `char_id` = Cur.char_id
END LOOP
CLOSE Cur
END
Which results in a syntax error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BEGIN
DECLARE Cur = CURSOR FOR
SELECT `char_id` FROM `life_insurance...' at line 7
I've done a lot of google searches regarding this, a lot of which stated I needed to use a delimiter. Upon trying that, with the following code, I get an error as well.
delimiter $$
CREATE EVENT lifeinsurance_reset
ON SCHEDULE EVERY 1 HOUR
STARTS CURRENT_TIMESTAMP()
ON COMPLETION NOT PRESERVE
ENABLE
BEGIN
DECLARE Cur = CURSOR FOR
SELECT `char_id` FROM `life_insurance` WHERE `renewal_date` <= CURRENT_TIMESTAMP()
OPEN Cur
LOOP
UPDATE `characters` SET `bank` = `bank` - 25000 WHERE `id` = Cur.char_id;
UPDATE `life_insurance` SET `renewal_date` = DATE_ADD(NOW(), INTERVAL 1 MONTH) WHERE `char_id` = Cur.char_id
END LOOP
CLOSE Cur
END$$
delimiter;
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'delimiter $$
CREATE EVENT lifeinsurance_reset
ON SCHEDULE EVERY 1 HOUR
STARTS...' at line 1
I'm not very well-versed in SQL, so any help would be appreciated.
For context, I'm trying to loop through all the rows in an insurance table for a game, if the renewal date is past current timestamp, it'll auto deduct money from the characters bank and reset the renewal date to a month from now.

Forms 6i if record exists then update

I need to create select statement in post_insert trigger. Is it possible if yes then how?
I want to check another table records if it exists then it will update it otherwise insert as new record. Please help.
My block code is that i want to run
DECLARE
EXSIST_TYPE varchar2(50);
EXSIST_NAME varchar2(50);
EXSIST_COMPANY VARCHAR2(100);
BEGIN
SELECT PRO_TYPE, PRO_NAME, COMPANY_NAME INTO EXSIST_TYPE, EXSIST_NAME ,EXSIST_COMPANY FROM STOCK;
IF
:PURCHASE_DETAIL.PRO_TYPE <> EXSIST_TYPE AND
:PURCHASE_DETAIL.PRO_NAME <> EXSIST_NAME AND
:PURCHASE_DETAIL.COMPANY_NAME <> EXSIST_COMPANY THEN*/
IF
:PURCHASE.RADIO_TYPE = 'PURCHASE' THEN
INSERT INTO STOCK(
PRO_TYPE ,
PRO_NAME ,
COMPANY_NAME ,
QUANTITY ,
PURCHASE_RATE,
SALE_RATE ,
RACK_NUM
)
VALUES
(
:PURCHASE_DETAIL.PRO_TYPE,
:PURCHASE_DETAIL.PRO_NAME,
:PURCHASE_DETAIL.COMPANY_NAME,
:PURCHASE_DETAIL.QUANTITY,
:PURCHASE_DETAIL.PRICE,
:PURCHASE_DETAIL.SALE_PRICE,
:PURCHASE_DETAIL.RACK_NUM
);
END IF;
ELSIF
:PURCHASE_DETAIL.PRO_TYPE = EXSIST_TYPE AND
:PURCHASE_DETAIL.PRO_NAME = EXSIST_NAME AND
:PURCHASE_DETAIL.COMPANY_NAME = EXSIST_NAME THEN
IF
:PURCHASE.RADIO_TYPE = 'PURCHASE' THEN
UPDATE STOCK SET
STOCK.QUANTITY = STOCK.QUANTITY+:PURCHASE_DETAIL.QUANTITY
WHERE
STOCK.PRO_TYPE = :PURCHASE_DETAIL.PRO_TYPE AND
STOCK.PRO_NAME = :PURCHASE_DETAIL.PRO_NAME AND
STOCK.COMPANY_NAME= :PURCHASE_DETAIL.COMPANY_NAME;
ELSIF
:PURCHASE.RADIO_TYPE = 'PRCH_RETURN' THEN
UPDATE STOCK SET
STOCK.QUANTITY = STOCK.QUANTITY-:PURCHASE_DETAIL.QUANTITY
WHERE
STOCK.PRO_TYPE = :PURCHASE_DETAIL.PRO_TYPE AND
STOCK.PRO_NAME = :PURCHASE_DETAIL.PRO_NAME AND
STOCK.COMPANY_NAME = :PURCHASE_DETAIL.COMPANY_NAME;
END IF;
END IF;
END;
You never said what happened when you ran that code.
Anyway: requirement you mentioned ("if it exists then it will update it otherwise insert") looks like an excellent candidate for a MERGE statement (also called upsert, as a combination of UPdate and inSERT).
As Forms 6i is an old piece of software, I'm pretty much sure that MERGE can't directly be used there. However, if the underlying database is at least 9i, MERGE will work - create a stored procedure that contains MERGE, and pass form items' values as parameters.
Here's an example (taken from here; have a look for more examples. I'm lazy to create my own code):
MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);

SQLite update trigger issue

Using SQLite Manager I'm trying to create a trigger that updates a field based on another field being changed:
CREATE TRIGGER staff_update
AFTER UPDATE OF c_doctor ON tickets
WHEN OLD.c_doctor <> NEW.c_doctor
BEGIN
UPDATE tickets
SET c_doctor_staff_contact = doctor_staff_contact.c_doctor_staff_contact
WHERE NEW.c_doctor = doctor_staff_contact.c_doctor;
END
c_doctor is the name of the field in the tickets table as well as doctor_staff_contact table. doctor_staff_contact is also the name of a field in those tables.
SET c_doctor_staff_contact = "test"; END works, so my assumption is = doctor_staff_contact.c_doctor_staff_contact WHERE NEW.c_doctor = doctor_staff_contact.c_doctor; END is giving me issues. As far as I can tell I'm doing it right.
= c_doctor_staff_contact IN (SELECT c_doctor_staff_contact FROM doctor_staff_contact
WHERE NEW.c_doctor = doctor_staff_contact.c_doctor);
END
This works but changes c_doctor_staff_contact to 0, and changes all the other tickets c_doctor_staff_contact to 0 as well.
The UPDATE query in the trigger body should look like this:
CREATE TRIGGER staff_update
AFTER UPDATE OF c_doctor ON tickets
WHEN OLD.c_doctor <> NEW.c_doctor
BEGIN
UPDATE tickets
SET c_doctor_staff_contact = (
SELECT c_doctor_staff_contact
FROM doctor_staff_contact
WHERE NEW.c_doctor = c_doctor
)
WHERE NEW.c_doctor = c_doctor;
END;

Set a due date column using a trigger

I'm working on a Sqlite database where a table has both a initial_date column and a due_date one. I'd like to automatically set the latter's value using a trigger every time the former one's value is changed, but something doesn't works.
Here's a simplified versione of the table DDL
CREATE TABLE timetable (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
initial_date DATETIME,
due_date DATETIME
);
As you can see, the two DATETIME columns allow NULL value; this is because in the real table I'd like to insert rows without setting the initial_date and update those rows later for setting the initial_date.
This is the trigger I added
CREATE TRIGGER [timetable-due-date] AFTER UPDATE ON timetable
BEGIN
UPDATE timetable
SET due_date = DATE( NEW.initial_date, '+ 10 days' )
WHERE id = NEW.id
AND NEW.initial_date IS NOT NULL;
END;
but it doesn't fires. If I execute UPDATE timetable SET initial_date='2013-10-04' WHERE id=1, due_date keeps the initial NULL value.
I also tried using the CREATE TRIGGER ... AFTER UPDATE OF initial_date ON ... variant, but without any luck.
I'm surely doing something really stupid here, but I can't figure what.
Thank you for your help.
The syntax of your date modifier is wrong; there must be no space between the + and the number:
CREATE TRIGGER "timetable-due-date"
AFTER UPDATE OF initial_date ON timetable
WHEN NEW.initial_date IS NOT NULL
BEGIN
UPDATE timetable
SET due_date = DATE(new.initial_date, '+10 days')
WHERE id = NEW.id;
END;

Error for displaying last login of user by using SQL Server procedure?

We are storing last 3 login timings of a user in our database. After that we using following procedure to get the last login detail of user. Unfortunately we are getting the current login date and timing on user login dashboard. Please help us to correct following procedure:
ALTER PROCEDURE [dbo].[gspVEBUserLastLoginDetails_GetLastLogin]
(#userLastLoginUserId bigint = null)
AS
select Top 1 NewDate
from
(SELECT
TOP 2 (CONVERT(varchar(26), UserLastLogin_Date,107)+' '+RIGHT(CONVERT(VarChar, UserLastLogin_Date, 100), 7)) as NewDate
FROM
[dbo].[VEB_UserLastLoginDetails]
WHERE
(#userLastLoginUserId IS NULL OR [UserLastLogin_UserId] = #userLastLoginUserId)
ORDER BY
NewDate DESC) AS t1
Try:
ALTER PROCEDURE [dbo].[gspVEBUserLastLoginDetails_GetLastLogin]
(
#userLastLoginUserId bigint = null
)
AS
BEGIN
IF COALESCE(#userLastLoginUserId,0)<>0
BEGIN
SELECT MAX(VEB_UserLastLoginDetails.UserLastLogin_Date)
FROM VEB_UserLastLoginDetails
WHERE VEB_UserLastLoginDetails.UserLastLogin_UserID = #userLastLoginUserID;
END
ELSE
BEGIN
SELECT MAX(VEB_UserLastLoginDetails.UserLastLogin_Date)
FROM VEB_UserLastLoginDetails;
END
END
GO
I'm making some assumptions here. The Table I used is:
CREATE TABLE VEB_UserLastLoginDetails
(
UserLastLogin_Date datetime
, UserLastLogin_UserID INT
);
And I inserted several rows using:
INSERT INTO VEB_UserLastLoginDetails VALUES (GETDATE(), 1);
GO 3;
I then executed the stored procedure using:
EXEC gspVEBUserLastLoginDetails_GetLastLogin 1;
which returns the maximum date which is the most recent login, naturally.
If you execte the stored procedure without passing a UserID, as in:
EXEC gspVEBUserLastLoginDetails_GetLastLogin;
it returns the most recent login, regardless of which user actually logged in.

Resources