Autnomous transaction SQL server2008 - sql-server-2008

I want to update a column on the inserted row with a trigger (SQL server):
example:
I have a table employee (firstname, lastname, score1, score2, total)
Iwant to create a trigger that after insert a new row (firstname, lastname, score1, score2) row into the table, set the score1+score2 value to the column total of the same inserted row

Related

Mysql triggers passing data from one table to another matching tables

I've table1 with columns (id, firstname, lastname) and table2 with columns (id, firstname, lastname, ans3, mandate, matchid). So far i've made this trigger that inserts firstname and lastname from table1 to table2. Now the thing that i want is to join the matchid from table2 with the id from table1. For example when matchid = 1 display records from table1 with id = 1 and in the same row the table2 records. Here's what i do so far with the trigger.
DELIMITER //
CREATE TRIGGER after_user_insert AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES (NEW.firstname, NEW.lastname, NEW.id);
END; //
DELIMITER ;
Here's a photo of what i get in phpmyadmin. You can see that i didn't figure how to join table2.matchid to table1.id
---edit---
From your comments it sounds like perhaps you intended to update a user record which already exists in table2. If so, you can use ON DUPLICATE KEY when you update:
DELIMITER //
CREATE TRIGGER after_user_insert AFTER INSERT ON table1
FOR EACH ROW
BEGIN
INSERT INTO table2 (firstname, lastname, matchid)
VALUES (NEW.firstname, NEW.lastname, NEW.id)
ON DUPLICATE KEY UPDATE matchid = NEW.id;
END; //
DELIMITER ;
This solution would require that there is a unique index on both the first and last name in table2. If you don't have such a unique index, then you can add one:
ALTER TABLE table2 ADD UNIQUE unq_idx (firstname, lastname);

MySQL DB Trigger Insert after cost linked to id

first Trigger I created.
I have two tables, student and cost.
If I insert a new student I want to automatically insert a cost row for this student in the table cost and insert the corresponding student id to the cost.
I donĀ“t know how I can link the student id to the cost...
CREATE TRIGGER `add_cost` AFTER INSERT ON `student` FOR EACH ROW INSERT INTO cost (amount) VALUES (2000)
Thanks everybody!!
In your insert statement please use just:
NEW.{primary_key}
from related table(in your case student). If your primary is id this should looks like:
NEW.id
NEW is created after row is inserted and contain all inserted values + PK after insert.
Finally your query should looks like below:
INSERT INTO cost (student_id, amount) VALUES (NEW.id, 2000)
You can find more info on http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Can an auto-incremented value in MySQL be preserved?

How do I preserve the last_insert_id() into a specific record?
Here are my queries:
INSERT INTO student(student_name) VALUES ('ramgopal')`;
SELECT `last_insert_id()`;
INSERT INTO grades (student_id,grade) VALUES (last_insert_id(),'A');
INSERT INTO class (semester,day,time) VALUES ('spring 2015','tuesday','12');
SELECT last_insert_id();
insert INTO grades(class_id) VALUES (last_insert_id());
In grade table I got grade_id, class_id, student_id and grade records.
When I use the last_insert_id() again it is just inserting the latest id and the previous id is not inserting. So I would like to preserve the initial last_insert_id for further use.

How to use procedure variable in select and insert query in MySQL?

I have two variables
1. inserted_user_id,
2. inserted_address_id
in my MySQL procedure.
I need to use them in insert queries and select queries. Im trying something like
insert into user_new(name, company, email, customer_id) select name,
company, email, inserted_user_id from user_old;
insert into user_address_map(address_id, user_id) select
inserted_user_id,inserted_address_id ;
There two statements are not working. How to use procedure variable's value in the above sql statements?
First make a select to get all the info: (you need to declare these variables first)
SELECT name,
company,
email,
INTO varname, varcompany, varemail
FROM user_old
then use it into insert
INSERT INTO user_new
(name,
company,
email,
customer_id)
VALUES (varname,
varcompany,
varemail,
inserted_user_id);
edit:
First insert the Selected values and get the id of the inserted row
INSERT INTO user_new
(name,
company,
email)
SELECT name,
company,
email
FROM user_old
SET out_param = last_insert_id();
Then update this row with your param
UPDATE user_new
SET customer_id = inserted_user_id
WHERE id = out_param;

2 Dates from the same column in one SQL query

I'm trying to work out an average between of time between two dates in the same field.
Basically i've got a transaction date and an id for each transaction and a customer id for each transaction.
I need to get the time between the first transaction and the second transaction. I dont mind working out the average between the two in excel but I dont know how to pull two dates from the same field.
transaction.created_at of the first transaction minus transaction.created_at of the second transaction for each and every customer in the database. I can pull the date of a transaction like
select
customer.id,
transaction.created_at
count(transaction.id)
from transaction
having count(transaction.id) = 2
Thanks
Not sure if this will always be Having count(*) = 2? If so, I think you could just use min and max, no?
/*
create table dbo.Example (tran_id int,created_at datetime,cust_id int)
insert dbo.example values (1,'10/1/2012',900)
insert dbo.example values (2,'10/2/2012',901)
insert dbo.example values (3,'10/18/2012',590)
insert dbo.example values (4,'10/10/2012',676)
insert dbo.example values (5,'10/11/2012',123)
insert dbo.example values (6,'10/17/2012',456)
insert dbo.example values (7,'10/9/2012',901)
insert dbo.example values (8,'10/30/2012',900)
insert dbo.example values (9,'10/4/2012',456)
insert dbo.example values (10,'10/17/2012',676)
*/
select
cust_id,
max([created_at]) as [Last Date],
min([created_at]) as [First Date],
datediff(hh,min([created_at]) ,max([created_at])) as [Hours diff]
from example
group by cust_id
having count(*) = 2
order by cust_id
Try the following to retrieve particular customer transactions count:
Select
Customer.id,
count(transaction.id)
from transaction
where Customer.id = '10'
Here 10 specifies the customer id that has been searched by you. You have to pass this customer id as parameter in your created SP.