IF ELSE condition with SQL Server trigger - sql

I am populating a table entirely using triggers, it populates the table if LocationID and ProductID does not exists and if it already does it updates the given data.
I have posted the following code snippet looking for a possible solution or link to one.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[newpurchase]
ON [dbo].[PurchaseMaster]
AFTER INSERT
AS
BEGIN
IF (((SELECT COUNT(*) FROM StockMaster
WHERE LocationID = (SELECT LocationID FROM inserted)) > 0)
AND ((SELECT COUNT(*) FROM StockMaster
WHERE LocationID = (SELECT LocationID FROM inserted)) > 0))
UPDATE StockMaster
SET TotalPurchased = TotalPurchased + (SELECT PurchasedQTY FROM inserted)
WHERE LocationID = (SELECT LocationID FROM inserted)
AND ProductID = (SELECT ProductID FROM inserted);
ELSE
INSERT INTO StockMaster (LocationID, ProductID, TotalPurchased, TotalSold, OnHand)
SELECT LocationID, ProductID, PurchasedQTY, 0, 0 FROM inserted;
END

I am not entirely sure exactly what you are looking for. If your return sets cannot be collected dynamically, and you need another table to always update based off the information persisted by another another table then I do think triggers are fine. There is always overhead with triggers, so keep that in mind.
You could do the way you are doing it, and from what I can see on the surface level the trigger may be what you are looking for (but this also depends upon your business needs and relationships of tables). HOWEVER, you will have to be careful if your Insert is a bulk insert then what you have will throw an error.
I also noticed your If statement is checking LocationId twice instead of LocationId and ProductId.
The trigger you are trying to create works with only one value at a time. You could rewrite this to a more set based trigger for when bulk inserts occur. This way if you have one insert or bulk inserts these two queries can perform for that values where needed.
first you want to update all the values where LocationId and ProductId are the >same found in StockMaster
if no values match then nothing will be updated
Update StockMaster
Set TotalPurchased = sm.TotalPurchased + i.PurchasedQty
From StockMaster sm
inner join inserted i on sm.LocationId = i.LocationId and sm.ProductId = i.ProductId
next you want to insert any row that isn't found in StockMaster
if all values matched above, they will be weeded out with the wheree condition >and,then nothing would be inserted here, those that didn't match would be
inserted
INSERT INTO StockMaster (LocationID, ProductID, TotalPurchased, TotalSold, OnHand)
SELECT i.LocationID, i.ProductID, i.PurchasedQTY, 0, 0
FROM inserted i
left join StockMaster sm on i.LocationId = sm.LocationId and i.ProductId = sm.ProductId
where sm.{Id} is null;--not {id} is for whatever key this table uses

Related

Make a trigger to only trigger if a certain column is updated

I'm trying to make a trigger to only trigger if a certain column is updated, and then only if the column is updated to 'Executed'. I can update if the column is changed, but I can't seem to find a way to update if column is updated to 'Executed'
CREATE TRIGGER dbo.NewTrigger
ON dbo.Database
AFTER UPDATE
AS
IF Update(Status) = 'Executed'
BEGIN
--MY insert into statement. This adds data to another table, but I only want the whole process to run if the original table column "Status" is set to "Executed"
END
Could someone assist please?
You'd need to use the inserted and deleted tables in the trigger, see here:
Use Inserted and Deleted Tables
In case of an update:
inserted table: contains new column values of rows that have been updated
deleted table: contains old column values of rows that have been updated
Your trigger could look something like this:
create table t (id int identity, status varchar(100));
create table audit(id int, old_status varchar(100), new_status varchar(100), updated_at datetime);
create trigger StatusUpdate
on t
After UPDATE as
if (update(status)
and exists(select * from inserted i
inner join deleted d on d.id = i.id
where d.status != 'Executed'
and i.status = 'Executed'))
begin
insert into audit (id, old_status, new_status, updated_at)
select i.id, d.status, i.status, getdate()
from inserted i
inner join deleted d on d.id = i.id
where d.status != 'Executed'
and i.status = 'Executed'
end
See Demo on DB Fiddle
Demo 2 - Multiple rows updated together

Pre-Populate foreign keys in SQL with a new row

I am trying to write a SQL script which will go through a table with a newly created FK and pre-populate the key with a new row in the foreign table. I'm not 100% on how to do this or if its even possible in a single statement but here's my attempt:
UPDATE [dbo].[Blogs]
set AuthorSecurableId = inserted.Id
FROM [dbo].[Blogs] updating
INNER JOIN
(INSERT INTO [dbo].[Securables] (Name)
OUTPUT Inserted.id, ROW_NUMBER() OVER (ORDER BY Inserted.Id) as rownum
SELECT 'Admin : ' + Name
FROM Blogs
WHERE AuthorSecurableId is null) inserted ON ROW_NUMBER() OVER (ORDER BY updating.Id) = inserted.rownum
WHERE updating.AuthorSecurableId is null
When I do this I get the following error
Msg 10727, Level 15, State 1, Line 5
A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed on either side of a JOIN or APPLY operator.
Below is a simple view of the schema I have
I would like to create a securable for each blog which doesn't have one and populate that blogs AuthorSecurableId with the ID of the newly created securable
I think I could do this with a cursor but I was wondering if there is a better single statement approach
You can use merge against Securables with output to a table variable or temp table and a separate update against Blogs.
declare #T table
(
SecID int,
BlogID int
);
merge Securables T
using (
select 'Admin : '+B.Name as Name, B.Id
from Blogs as B
where B.AuthorSecurabledId is null
) as S
on 0 = 1
when not matched by target then
insert (Name) values (S.Name)
output inserted.Id, S.Id
into #T;
update Blogs
set AuthorSecurabledId = T.SecID
from #T as T
where Blogs.Id = t.BlogID;
SQL Fiddle

SQL Update in Insert clause

I have a query where I am inserting some records as long as those records do not exist in a table, but I would really like to do is to UPDATE a field of that record in that other table if it is there otherwise insert it as it is doing it right now. I would appreciate any help, or advises. Can it be done?
-- Insert into my my Toys table if record not already there (but would like to also update one of its fields)
INSERT INTO Toys (Date, ToyId)
SELECT inv.Date, inv.Id
FROM Inventory inv
JOIN InventoryStats invSt ON inv.Id = invSt.InventoryId
WHERE (invSt.IsFixed = 1 AND invSt.IsSent = 0 AND invSt.Date >= '01/01/2012 12:00 PM') AND (inv.Id NOT IN (SELECT ToyId FROM Toys))
Basically if the inv.Id is already in Toys table I do not want to insert it again but I would like to update one of its flags while on this process , like
UPDATE Toys SET NewShipDate = inv.Date WHERE ToyId = Inv.Id
You are looking for the MERGE Statement - INSERT a record if it does not exist, UPDATE it if it does.

SQL trigger on INSERT, and DELETE with 2 Tables

I've 2 tables called Patient and Diagnosis. as follows
Patient Diagnosis
--------- ------------
ID (PK) ID (PK)
Name PatientID (FK: Reference to Patient => ID)
Status
****** *****
---------- -----------
Here, Patient Status may be [Registered, Diagnosed and OnCourse]
During,
New Patient Insert, Patient Status will be Registered
New Diagnosis Insert, Patient Status will be Diagnosed
Now, On
Diagnosis Delete, I need to check If Patient has at least one Diagnosis entry in Diagnosis table, then Patient Status will be Diagnosed otherwise Registered
So, How to do all these conditions in Single Trigger?
Please help me.
Trigger to update Patient.Status based on INSERTs and DELETEs on the Diagnosis Table.
CREATE TRIGGER dbo.Diagnosis_TrgInsDel
ON dbo.Diagnosis
AFTER DELETE, INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Change the Status to 'Registered' after the last
-- Diagnosis record is deleted
--------------------------------------------------------------------------------
UPDATE
Patient
SET
[Status] = 'Registered'
FROM
Patient
INNER JOIN
Deleted
ON Deleted.PatientID = Patient.ID
WHERE
NOT EXISTS (SELECT * FROM Diagnosis WHERE PatientID = Deleted.PatientID)
-- Change the Status to 'Diagnosed' after an Insert and the
-- current Status is 'Registered'
--------------------------------------------------------------------------------
UPDATE
Patient
SET
[Status] = 'Diagnosed'
FROM
Patient
INNER JOIN
Inserted
ON Inserted.PatientID = Patient.ID
WHERE
Patient.[Status] = 'Registered'
END
I would actually have don this as two triggers. One for AFTER DELETE and one for AFTER INSERT. That would mean that the DELETE code wouldn't run when there is an INSERT and vice versa. But the above code will indeed work correctly.
EDIT
AS spotted by Nikola; if multiple diagnosise are inserted or updated, in the same operation, for the same patient, then this will potentially update a single patient record multiple times. These modifications should address that...
UPDATE
Patient
SET
[Status] = 'Registered'
WHERE
NOT EXISTS (SELECT * FROM Diagnosis WHERE PatientID = Patient.ID)
AND EXISTS (SELECT * FROM Deleted WHERE PatientID = Patient.ID)
And...
UPDATE
Patient
SET
[Status] = 'Diagnosed'
WHERE
Patient.[Status] = 'Registered'
AND EXISTS (SELECT * FROM Inserted WHERE PatientID = Patient.ID)
For the first part, you may simply add default constraint:
alter table patient add constraint df_status default 'Registered' for status
If this would not be enough because your front end is incapable of omitting status in insert or setting value to DEFAULT, you can create a trigger:
create trigger PatientInsertTrigger on patient
after insert
as
-- trigger should not alter ##rowcount
set nocount on
update patient
set status = 'Registered'
from Patient
-- Inserted is a pseudotable holding newly inserted rows
-- This is how we know what records to update
inner join Inserted
on Patient.ID = Inserted.ID
When records are added to or removed from diagnosis, patient status should be updated according to number of matching records in diagnosis. Luckily, at the time trigger is invoked records are already in table, so it is sufficient to take count() in both cases.
create trigger DiagnosisTrigger on diagnosis
after insert, delete
as
set nocount on
update patient
set status = case when d.NumberOfDiagnosis <> 0
then 'Diagnosed'
else 'Registered'
end
from Patient
inner join
(
select PatientID, count(*) NumberOfDiagnosis
from Diagnosis
-- Get all patients with added or removed diagnosis
where PatientID in (select PatientID
from Inserted
union
select PatientID
-- Pseudotable holding removed records
from Deleted)
group by PatientID
) d
on Patient.ID = d.PatientID
Status should be StatusID, but you did not mention appropriate id numbers.

Writing a complex trigger

I am using SQL Server 2000. I am writing a trigger that is executed when a field Applicant.AppStatusRowID
Table Applicant is linked to table Location, table Company & table AppStatus.
My issue is creating the joins in my query.
When Applicant.AppStatusRowID is updated, I want to get the values from
Applicant.AppStatusRowID, Applicant.FirstName, Applicant.Lastname, Location.LocNumber, Location.LocationName, Company.CompanyCode, AppStatus.DisplayText
The joins would be :
Select * from Applicant A
Inner Join AppStatus ast on ast.RowID = a.AppStatusRowID
Inner Join Location l on l.RowID = a.LocationRowID
Inner Join Company c on c.RowID = l.CompanyRowID
This is to be inserted into an Audit table (fields are ApplicantID, LastName, FirstName, Date, Time, Company, Location Number, Location Name, StatusDisposition, User)
My issue is the query for the inner join...
First lets introduce you to the inserted and deleted pseudotables which are available only in triggers. Inserted has new values and delted has old values or records being deleted.
You do not want to insert all records into your audit table only those in inserted.
So to insert into an audit table you might want something like inside the trigger code:
insert Myaudittable (<insert field list here>)
Select <insert field list here> from Inserted i
Inner Join AppStatus ast on ast.RowID = i.AppStatusRowID
Inner Join Location l on l.RowID = i.LocationRowID
Inner Join Company c on c.RowID = l.CompanyRowID
I would personally add columns for old and new values, a column for the type of change and what the date of the change and what user made the change, but you I'm sure have your own requirement to follow.
Suggest you read about triggers in Books online as they can be tricky to get right.
Here's one way to test and debug trigger that I often use. First I create temp tables names #delted and #inserted that have the sturcture of the table I'm going to put the trigger on. Then I write the code to use those instead of the deleted or inserted tables. That wa y I can look at things as I go and make sure everything is right before I change the code to a trigger. Example below with you code added in and modified slightly:
Create table #inserted(Rowid int, lastname varchar(100), firstname varchar(100), appstatusRowid int)
Insert #inserted
select 1, 'Jones', 'Ed', 30
union all
select 2, 'Smith', 'Betty', 20
Create table #deleted (Rowid int, lastname varchar(100), firstname varchar(100), appstatusRowid int)
Insert #deleted
select 1, 'Jones', 'Ed', 10
union all
select 2, 'Smith', 'Betty', 20
--CREATE TRIGGER tri_UpdateAppDisp ON dbo.Test_App
--For Update
--AS
--If Update(appstatusrowid)
IF exists (select i.appstatusRowid from #inserted i join #deleted d on i.rowid = d.rowid
Where d.appstatusrowid <> i.appstatusrowid)
BEGIN
--Insert AppDisp(AppID, LastName, FirstName, [DateTime],Company,Location,LocationName, StatusDisp,[Username])
Select d.Rowid,d.LastName, d.FirstName, getDate(),C.CompanyCode,
l.locnum,l.locname, ast.Displaytext, SUSER_SNAME()+' '+User
From #deleted d
Join #inserted i on i.rowid = d.rowid
--From deleted d
--Join inserted i on i.rowid = d.rowid
Inner join Test_App a with (nolock) on a.RowID = d.rowid
inner join location l with (nolock) on l.rowid = d.Locationrowid
inner join appstatus ast with (nolock) on ast.rowid = d.appstatusrowid
inner join company c with (nolock) on c.rowid = l.CompanyRowid
Where d.appstatusrowid <> i.appstatusrowid)
end
Once you get the data for the select correct, then it is easy to uncomment out the trigger code and the insert line and change #deleted or #inserted to deleted or inserted.
You'll note I had two records in the temp tables, one of which met your condition and one of which did not. This allows you to test mulitple record updates as well as results that meet the condition and ones that don't. All triggers should be written to handle multiple records as they are not fired row-by-row but by batch.