Trigger in SQL Server 2008 R2 - sql-server

I am trying to create a trigger in SQL Server 2008 R2. I have three tables and they have PK-FK -FK relationship.
I have a column in first table where column name is STATUS that column has value 'inactive' or 'active' and in second table I have column FKNEWLoanreviewfieldid which has multiple values C2,C4,C6 and in third table I have FKWarningtypeID has different value like 4,3,2,1.
I have to create a trigger so that if FKwarningtypeID is updated to value 4 then in first table (where I have status column) the column value of status will be modified to 'active'.
Can any body help me please.

You need something like this:
CREATE TRIGGER trg_UpdateYourColumn
FOR UPDATE ON dbo.YourThirdTableHere
AS
-- update the first table
UPDATE
dbo.YourFirstTableHere
SET
-- set [Status] to 'Active'
-- (I sure hope this isn't *REALLY* stored as a *STRING* value!)
[Status] = 'Active'
FROM
dbo.YourFirstTableHere t1
INNER JOIN
-- join first and second table on some common column
dbo.YourSecondTableHere t2 ON t1.XX = t2.XX
INNER JOIN
-- join second and third table on some common column
dbo.YourThirdTableHere t3 ON t2.YY = t3.YY
INNER JOIN
-- join third table with the "Inserted" pseudo table
Inserted i ON t3.ID = i.ID
INNER JOIN
-- join third table with the "Deleted" pseudo table
Deleted d ON t3.ID = d.ID
WHERE
i.FKwarningtypeID = 4 -- new value is 4
AND d.FKwarningtypeID <> 4 -- old value was *NOT* 4

Related

SQL Server : update old ID to new ID (SELF JOIN in UPDATE)

I have a SQL Server table, and column category IDs (kategori) needs to be updated.
I need to find id_old first and update as new id instead:
How can I do this in batch with a T-SQL statement?
use SELF JOIN. You need to use a different alias on the table.
UPDATE t2
SET kategori = t1.id
FROM yourtable t1
INNER JOIN yourtable t2 ON t1.id_old = t2.kategori
WHERE t2.kategory <> 0 -- not sure but you might need this condition

How to set trigger based on update query in SQL Server?

I am trying to set trigger whenever phone is updated in table1 and then do operation like check phone from table2 and if present, pick up the employee code from table2 and then update employee code in table1.
My code for trigger:
CREATE TRIGGER UPDATED_Contact_Trigger
ON table1
AFTER UPDATE
AS
DECLARE #tuid bigint;
DECLARE #phone varchar(15);
DECLARE #employee_code varchar(10);
IF (UPDATE (phone)) --Phone is the Column Name
BEGIN
SELECT #employee_code = i.emp_code
FROM table2 i (nolock)
WHERE i.MOBILE_NO = #phone
UPDATE table1
SET client_code = #employee_code
WHERE tuid= #tuid
END;
This trigger is set, but there is no update on table1 even if I update a contact which is present in table2
You're not even looking at the Inserted or Deleted pseudo tables in your code - how do you want to know what rows have been updated??
Inserted is a pseudo table that contains all updated rows - and it contains the new values after the update operation, while Deleted contains the same rows, but with the old values before the update.
You need to do something like this:
join your Table1 to Inserted to get the rows that were updated (since you didn't show your table structure, I cannot know what your primary key on Table1 is - you need to use that to join to Inserted)
add a join to your Table2 and pick those rows that have been updated, and limit those to the ones that have been updated in the Phone column (by comparing the Inserted.Phone value to Deleted.Phone)
Try this code:
CREATE TRIGGER UPDATED_Contact_Trigger
ON table1
AFTER UPDATE
AS
BEGIN
-- update your base table
UPDATE t1
-- set the client_code to the employee_code from Table2
SET client_code = t2.employee_code
FROM dbo.Table1 t1
-- join to "Inserted" to know what rows were updated - use the primary key
INNER JOIN Inserted i ON t1.(primarykey) = i.(primarykey)
-- join to "Deleted" to check if the "Phone" has been updated (between Deleted and Inserted)
INNER JOIN Deleted d ON i.(primarykey) = d.(primarykey)
-- join to "Table2" to be able to fetch the employee_code
INNER JOIN dbo.Table2 t2 ON t2.mobile_no = t1.phone
-- ensure the "Phone" has changed, between the old values (Deleted) and new values (Inserted)
WHERE i.Phone <> d.Phone;
END;

Join inserted record with latest record target table

i'm setting up a trigger that, in case of a new record being added to my source table, inserts a record into my target table with information based on the data in the first inserted record.
Since the new data in the target table are basically calculated fields based on the original latest record in the target table and the inserted record in the source table, i basically need a way to join the last record of my target table and my INSERTED table.
i've tried selecting the top row of my target table and join this with my INSERTED table, but so far it has not worked
CREATE TRIGGER dbo.NewData ON dbo.t_stats_lijn1 AFTER INSERT AS
IF Exists(select top 1 Categorie from INSERTED where Categorie = 'geen stop' order by p_timestamp desc)
begin
declare #joincause int = 0
insert into dbo.T_VALUE_LIJN1 ([Event ID]
,[p_timestamp]
,[Model]
,[Modelnaam]
,[running_time]
)
select
i.event_id,
i.p_timestamp
i.model
t.running_time + i.delta
from inserted i left join (select top 1 * from t_value_lijn1 order by p_timestamp) t on #joincause = 0
end
go

Updating a column in SQL table where items are identified in another table that is linked

I have two tables in MSSQL.
Table1
Table2
I want to update the Status column in Table 1 to "YES" if the same Ticket ID, House and Part Number exists in Table 2. After updating, Table 1 should be like,
How can I achieve this?
Thanks
a simple EXISTS() will do the job
UPDATE t1
SET Status = 'Yes'
FROM Table1 t1
WHERE EXISTS
(
SELECT *
FROM Table2 t2
WHERE t1.TicketID = t2.TicketID
AND t1.House = t2.House
AND t1.PartNumber = t2.PartNumber
)
or an INNER JOIN will gives you the query that you want

SQL Server 2008 - Ambiguous column name

I am trying to do a join between two tables but I am receiving this error
Ambiguous column name 'NurseWard'
Code:
select
#WardID = NurseWard
from
dbo.NurseTbl as o
inner join
inserted as i on o.NurseID = i.NurseID
This looks like the inserted table in a trigger. The inserted table has the same column names as the original table. From the linked article:
The inserted table stores copies of the affected rows during INSERT
and UPDATE statements. During an insert or update transaction, new
rows are added to both the inserted table and the trigger table. The
rows in the inserted table are copies of the new rows in the trigger
table.
The query has an alias on both the NurseTbl and inserted tables, and the inner join uses the aliases, but the select does not. You will need to add an alias to the NurseWard column in the select:
select #WardID = o.NurseWard -- or i.NurseWard depending on what you need
from dbo.NurseTbl as o
inner join inserted as i on
o.NurseID = i.NurseID

Resources