Forum Discussion
Error when trying to create a Trigger
Hi
I am trying to create a trigger so when a value changes from null to a value it copies the value into another field. However, I am getting errors on the basic code like BEFORE.
Any ideas why?
Thanks
Aaron
Hi
I managed to resolve the issue. I think it was because the code I was using was mySQL and when I converted it to TSQL it works.
Thanks for trying to help.
Aaron
4 Replies
- olafhelperBronze Contributor
Your SQL code is PL/SQL from/for Oracle database; don't work in T-SQL
- AjcbutlerSeikiCopper Contributor
Hi
I managed to resolve the issue. I think it was because the code I was using was mySQL and when I converted it to TSQL it works.
Thanks for trying to help.
Aaron
- rodgerkongIron Contributor
Check out the SQL Server syntax of Trigger CREATE TRIGGER (Transact-SQL) - SQL Server | Microsoft Learn
- divyedCopper Contributor
Hello ,
This is not a valid syntax in SQL SERVER, looks like you're attempting more like MySQL or Oracle PL/SQL.
The BEFORE trigger and NEW-OLD pseudo records do not exist in SQL Server.
Here is a code in sql server you can use :
USE CORE_DATA;
GOCREATE TRIGGER trg_copy_two_fields
ON dbo.OR_OP
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON;UPDATE t
SET FIXSTIME = i.STIME
FROM dbo.OR_OP t
INNER JOIN inserted i ON t.PrimaryKey = i.PrimaryKey
INNER JOIN deleted d ON d.PrimaryKey = i.PrimaryKey
WHERE d.STIME IS NULL AND i.STIME IS NOT NULL;
END
GOReplace PrimaryKey with your actual primary key column.
To copy ETIME to FIXETIME, add below code
SET FIXSTIME = i.STIME,
FIXETIME = i.ETIMEI hope this will answer your query, if not let me know more about tool you are using.
Kind Regards,