SQL Server 2008 trigger throwing error when I use procedure GETDATE - sql-server-2008

I am trying to create a trigger in SQL Server 2008 which inserts a row into a 2nd database after an update in the 1st database.
However I keep getting an error..
(Procedure behaviour_alert, Line 11 Incorrect syntax near ')'
Is this because of using DATETIME in the trigger.
This works as a normal query, I can't see why it won't work as a trigger.
Can only adjust the query to choose the current datetime?
Query below
create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin
declare #conductdatetime as datetime
set #conductdatetime = GETDATE()
insert into database2.dbo.behaviouralert
select *
from studconduct
where curr_ind='Y'
and cond_pts >= '5'
and conduct_date >= #conductdatetime
What am I missing here, going crossed eyed looking at this. Maybe I have had too much coffee.
Edit: this is what I ended up with and it worked. I missed the END at the end of the trigger
create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin
insert into database2.dbo.behaviouralert
select *
from studconduct
where curr_ind='Y'
and cond_pts >= '5'
and conduct_date >= datetime
end

I think the problems is not in GETDATE().
maybe you just forget about END in the end of trigger?

Related

How to add a trigger in Oracle SQL to update a value after a row was created

I am trying to create triggers for my Oracle database and I always get compiled with errors when trying to add it.
The statement is as follows
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking SET open_amount = open_amount - NEW.amount WHERE Booking.booking_id = NEW.booking_id;
END;
and I get the following error
2/5 PL/SQL: SQL Statement ignored
2/90 PL/SQL: ORA-00904: "NEW"."BOOKING_ID": invalid identifier
The trigger should subtract the amount in the newly created payment row from the open_amount field in the booking row and store the new value.
I managed to get it to work in mysql with the following statement
DELIMITER $$
CREATE TRIGGER `update_open_amount` AFTER INSERT ON `Payment`
FOR EACH ROW
BEGIN
UPDATE `Booking` SET `open_amount` = `open_amount` - NEW.`amount` WHERE `Booking`.`booking_id` = NEW.`booking_id`;
END;
$$
DELIMITER ;
but when I try to get it to work in oracle I get stuck.
How can I get the trigger to work because this is all that is still missing for migrating the database from mysql to oracle.
How can I fix this?
Thanks in advance
Please check syntax for referring new and old values. It should have preceeding colon (:)
CREATE OR REPLACE TRIGGER update_open_amount
AFTER INSERT ON Payment
FOR EACH ROW
BEGIN
UPDATE Booking
SET open_amount = open_amount - :NEW.amount
WHERE Booking.booking_id = :NEW.booking_id;
END;

How to Create a Trigger MySQL 5.6

we are having a problem with the created_at on a WordPress system. Somehow some users get that field updated "randomly" to an invalid value, so it get's set as '00000000' datetime. We haven't found the cause on the WP code, and we are now analizing the few plugins the project has. It has a large Code and user base, so we thought it might be faster to use a MySQL trigger to catch this "random" updates.
The thing is we somehow keep on hitting a syntax error on or Trigger code, and lame SQLer's as we are, we need help trying to figure out what it could be.
What we are tying to accomplish is:
Detect when the user table gets updated
Check if the created_at row has been modified
Insert the old data into a new table we created just for this purpose (user_registration_changed_records).
We decided to check for each row just in case there is some weird behaviour going on.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at)
END IF;
END;$$
DELIMITER ;
Thanks in advance for any suggestions!
Syntax Error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.I' at line 6
You have a couple of syntax errors:
missing then keyword before the insert (this is the one the error message relates to)
missing ; after the insert's row
extra ; after the end
At least these are the ones I can see.
DELIMITER $$
CREATE TRIGGER `user_register_updated` BEFORE UPDATE ON `wp_t8y31tcd9u_users`
FOR EACH ROW
BEGIN
IF OLD.created_at != NEW.created_at THEN
INSERT INTO user_registration_change_records (user_id, created_at) VALUES (OLD.ID, OLD.created_at);
END IF;
END$$
DELIMITER ;
It is worthwile using mysql manual on compound statement syntax because all these can be found there.

How to correct a MySQL "BEFORE INSERT" trigger's syntax?

I'm creating a trigger from the table ItemBorrower that is meant to update a field in the table Item. When ItemBorrower gets a new entry inserted, the currentDate field in the Item table should be updated to today's date. Here is my trigger:
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW UPDATE Item
BEGIN
SET Item.checkoutDate = CURDATE()
WHERE NEW.itemID = Item.itemID
END;
And there error I'm getting is about the last line:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'END' at line 8
I've played around with the delimeter (removing it, adding a ";" after Item.itemID), but to no apparent avail. Could be that I just did that totally wrong, too.
Any help appreciated!
Thanks,
Jona
I am not 100% sure what are you trying to accomplish from the trigger. I assumed that you have a table called item and you want to update the checkoutDate column with current date where the item.itemID = the last insert id in ItemBorrower.
Try this code
delimiter //
CREATE TRIGGER checkoutItem BEFORE INSERT ON ItemBorrower
FOR EACH ROW
BEGIN
UPDATE Item
SET checkoutDate = CURDATE()
WHERE itemID = NEW.itemID;
END; //
delimiter ;

MySQL Workbench error 1305 PROCEDURE doesn't exist

I'm trying to execute a statment in MySql to update a column in a table when the expiry date for one of the other columns surpasses the current date, this is then compared against something to make sure that there are no active people for it
but i keep getting this error , i can not see anything wrong with my syntax so im not sure what it is
Error 1305 PROCEDURE does not exist
UPDATE job j SET archived = 1 WHERE(SELECT count(*) FROM job_applied_candidates jac WHERE jac.jobID = j.id) = 0 AND enddate < now();
Add a space between WHERE an (Select...
Also check triggers on jobs table, since they could use a procedure that does not exist.

Set field to automatically insert time-stamp on UPDATE?

I have a table with a DEC field named product_price and I wanted to add a field called price_updated_date. Is there a way to set the table to automatically insert the current time-stamp whenever the product_price field has been updated?
If not, is there a way to set it to insert the current time-stamp any time the entry is updated at all?
update:
It seems like using a trigger is the best option here. I am new to triggers and having some trouble creating it. Here is my code:
CREATE TRIGGER price_update
AFTER UPDATE ON cart_product
FOR EACH ROW
IF(OLD.product_price != NEW.product_price)
THEN
UPDATE cart_product
SET price_updated_date = CURDATE()
WHERE product_id = NEW.product_id
This is giving me this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8
Why not set properties for that field as:
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
yup, create trigger after update on your table
CREATE TRIGGER price_update AFTER UPDATE on _table_ FOR EACH ROW UPDATE _table_ SET price_updated_date = CURTIME() where id=NEW.id
I note you're using MySQL
The default behaviour of a MySQL timestamp field is to default to NOW() on insert AND on update
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I know this isn't dependant on which column has been updated as you require, but may be of some use to you or someone else browsing this question.
Its a 5-second job - just add a timestamp field & hey presto
Anytime you run an update statement, make sure you do something like this:
UPDATE table SET `product_price` = 'whatever', price_updated_date = NOW()
This will always insert the current date/time when you change the row.
Use a database trigger:
http://dev.mysql.com/doc/refman/5.0/en/triggers.html