SQL server 2008 trigger before insert - sql-server-2008

I am very new to SQL and sorry for asking this is very primary knowledge. My issue as follow.
I have a table called Group and minimumVal and maximumVal are two columns. minimumVal always should be less than maximumVal. I want to check this before inserting a new record to the table. If this condition failed data should not inserted into the DB.
So as per my understanding I though of having a trigger but have no idea how to write this.
This is what I write so far;
CREATE TRIGGER tr_Group
ON Table_Group
for INSERT
AS
????
Please advise me.

I recommend you use check constraints for this:
ALTER TABLE dbo.Table_Group ADD CONSTRAINT CK_Group
CHECK (minimumVal < maximumVal)
If you still want to use triggers, then you need an INSTEAD OF trigger:
CREATE TRIGGER tr_Group ON Table_Group
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Table_Group
SELECT *
FROM INSERTED
WHERE minimumVal < maximumVal
END

Related

Using a trigger to automatically assign a value to specific column on a new insert

I'm struggling to find proper information on SQL Triggers.
My previous question got removed for it not being specific enough so hopefully this will do better.
I am trying to create a trigger that will assign RoleID 1 to every newly inserted row to my users table.
But I can't seem to figure it out.
AFTER INSERT on users
on EACH ROW
insert into users.RoleID values(1);
This doesn't seem to work.
And the examples and or information regarding triggers all focus on alerts or sending emails after an insert/drop or update.
Can it actually be done?
Any help would be much appreciated.
Cheers!
It looks like you aren't creating you sql trigger with the correct keyword.
Try this
drop trigger if exists before_insert_users;
DELIMITER $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SET NEW.RoleID = 1;
END$$
DELIMITER ;
Note: RoleID will need to actually be a column on your table for this to work.
However, a trigger is not the best way to do this... See this SO post : Add a column with a default value to an existing table in SQL Server

MySQL merge data on duplicate key

I'm adopting dashboard now and I created two tables for selecting from frontend;
DATA_SELECTED_HISTORY
DATA_SELECTED_NOW
My frontend page get data from DATA_SELECTED_NOW and my backend algorithm put new data to this database.
I want to put my new data to DATA_SELECTED_NOW,
and the former data to be pushed to DATA_SELECTED_HISTORY when being faced with duplicate key.
I think I could use a swap table solution or insert(select subquery) + insert on duplicate key solution, but I don't get an idea anymore.
How can I use this solution in SQL?
you can use trigger in this case, to check duplication before insert to DATA_SELECTED_NOW and insert in DATA_SELECTED_HISTORY if it duplicates, check the below code
CREATE TRIGGER TRIGGER_Name
BEFORE INSERT ON DATA_SELECTED_NOW
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT 1 FROM User WHERE key = NEW.Key)) THEN
-- you can replace "key = NEW.Key " with your logic to check
-- inset into DATA_SELECTED_HISTORY
END IF;
END$$

What is proper way to set and compare variable inside an sql trigger

Am populating a table using a trigger after an insert event occurs on another table and that worked fine. However i then noticed that the trigger would still insert a new row for existing records. To fix this, I want to create the trigger again but this time it would only fire if a condition is met...but not having previously used triggers in the past am getting a syntax error and not able to identify what am doing wrong. Kindly have a look and help me fix this
CREATE TRIGGER `students_gen_insert`
AFTER INSERT ON `students` FOR EACH ROW
BEGIN
INSERT INTO records (student_id, subject_id)
SELECT new.student_id, subjects.subject_id
FROM subjects
WHERE category = new.class;
END;
Am currently using MySql 5.6.17 version.
It is generally not a good idea to SELECT from the table the trigger is on, and forbidden to UPDATE or INSERT (not that you are doing those). Assuming you are trying to get the values for the row just inserted, the first SET ... SELECT you have is needless; just use NEW.fieldname to get the fields of the inserted row.
The second SET ... SELECT and following condition are a bit confusing. If referential integrity is being maintained, I would think it would be impossible for the records table to refer to that particular student_id of the students table at the point the trigger is executed. Perhaps this was to avoid the duplicate inserts from the trigger's previous code? If so, it might help for you to post that so we can pinpoint the actual source of redundant inserts.

Can you modify a foreign key on insert with a trigger?

I have an issue where a query is inserting into table A, which references table B, but it's using the wrong foreign key for table B. Is it possible to create a trigger in oracle where if the input foreign key is 'ASDF', we modify it to 'FDSA' before the insert so that we can fix this issue?
In either MySQL or Oracle, you can do this using a before insert trigger.
I don't recommend using a trigger for this purpose. Fixing the input data or adding the new value to the reference table seem to me to be more sensible approaches.
Following up on #GordonLinoff's post - I agree with (what appears to be) his point that a trigger is inappropriate here. However, recognizing that sometimes you have to work with what you're given, if you were to use a trigger you'd use a BEFORE INSERT trigger, as in:
CREATE TRIGGER TABLE_A_BI
BEFORE INSERT ON TABLE_A
FOR EACH ROW
BEGIN
IF :NEW.FK_FIELD = 'ASDF' THEN
:NEW.FK_FIELD = 'FDSA';
END IF;
END YOUR_TABLE_BI;
Best of luck.

how to create trigger on any change in table

I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END