SQL Trigger After Update Insert data into another table - sql

I have two table. One is customer_info and another one is update_log. I want to create a trigger that will execute after update in customer_info. If cust_name, Contact is updated then cust_no, Cust_name, Contact, Date will be added to second table. Second table will have two row with old data and new data. my code is not working. Any help will be highly appreciated.
CREATE TRIGGER update_trigger
AFTER UPDATE ON CUSTOMER_info
FOR EACH ROW
BEGIN
IF NEW.cust_name <> OLD.cust_name || NEW.contact <> OLD.contact then
insert into update_log values
( OLD.cust_no,OLD.cust_name, OLD.contact, CURRENT_DATE);
insert into update_log values
( NEW.cust_no,NEW.cust_name, NEW.contact,CURRENT_DATE);
END IF
END

If You use MSSQL, You can use that trigger:
CREATE TRIGGER update_trigger
ON CUSTOMER_info
FOR update AS
BEGIN
IF UPDATE (cust_name) OR UPDATE (contact)
BEGIN
INSERT INTO update_log (
cust_no
,Cust_name
,Contact
,Date
)
SELECT cust_no
,cust_name
,contact
,GetDate()
FROM INSERTED
INSERT INTO update_log (
cust_no
,Cust_name
,Contact
,Date
)
SELECT cust_no
,cust_name
,contact
,GetDate()
FROM DELETED
END
END
GO

Related

Error pls-00403 oracle

CREATE OR REPLACE TRIGGER TR_BCK_ANAG
BEFORE DELETE OR INSERT OR UPDATE ON ANAGRAFICA
FOR EACH ROW
BEGIN
SELECT ID ,
NOME ,
COGNOME ,
DATA ,
NUMERO_DI_TELEFONO ,
COMUNE
INTO BCK_ANAG
FROM ANAGRAFICA;
END;
Why does oracle give me back error
PLS-00403:expression 'BCK_ANAG' cannot be used as an INTO-target of a
SELECT/FETCH statement
and how can i solve this?
CREATE OR REPLACE TRIGGER TR_BCK_ANAG
BEFORE DELETE OR INSERT OR UPDATE
ON ANAGRAFICA
FOR EACH ROW
BEGIN
INSERT INTO BCK_ANAG (ID,
NOME,
COGNOME,
DATA,
NUMERO_DI_TELEFONO,
COMUNE)
SELECT :NEW.ID,
:NEW.NOME,
:NEW.COGNOME,
:NEW.DATA,
:NEW.NUMERO_DI_TELEFONO,
:NEW.COMUNE
FROM DUAL;
END;
I would assume this is what you're trying to do.

SQL FUNCTION - one alias in SELECT AND WHERE

I want to insert to NewTable some id(found by calledMethod) from table 'inserted' when this id actually it's not in this table(NewTable)
Actually i'm using this method (calledMethod)twice. How to reduce this using for example alias?
CREATE TRIGGER TriggerName
ON Table
AFTER INSERT
AS
BEGIN
INSERT INTO NewTable
(
FirstId
SecondId
)
SELECT
I.ID
CalledMethod(I.Name)
FROM INSERTED I
WHERE CalledMethod(I.Name)
NOT IN (SELECT SecondId FROM NewTable)
END
GO
The second problem occures when I want insert two rows at the same time.
Insert Into Table
(
Name
)
Values
('ro'),('ro-RO')
In this situation, the method returns the same index and both will be added. How to resolve this problem.
In this situation, the method returns the same index
This is example of calledMethod
CREATE FUNCTION CalledMethod
(
#internalName nvarchar(50)
)
RETURNS int
AS
BEGIN
return case
when #internalName Like 'ro%' then 6
when #internalName Like 'sk%' then 7
when #internalName Like 'bg%' then 9
end
END
I believe this is what you need:
CREATE TRIGGER TriggerName
ON Table
AFTER INSERT
AS
BEGIN
INSERT INTO NewTable
(
FirstId
SecondId
)
SELECT
min(I.ID),
x.called
FROM INSERTED I
CROSS APPLY
(SELECT CalledMethod(I.Name) called) x
WHERE
x.called NOT IN (SELECT SecondId FROM NewTable)
GROUP BY x.called
END

Inserting existing column value into a new table when updating the existing value in SQL Server

I'm new to SQL Server.
I need a one answer that it is, I have a table tblemployee and the columns are
empid,
name,
paddr1,
paddr2,
pcity,
pstate,
pzip,
caddr1,
caddr2,
ccity,
cstate,
czip
I have a another table tblpaddresshistory and the columns are
paddrlistid,
paddr1,
paddr2,
pcity,
pstate,
pzip
and I have yet another table tblcaddresshistory with these columns
caddrlistid,
caddr1,
caddr2,
ccity,
cstate,
czip
Here is my question: when I'm updating the tblemployee of column values paddr1, paddr2, pcity, pstate, pzip, caddr1, caddr2, ccity, cstate, czip, the old values should be inserted into tblpaddresshistory and tblcaddresshistory and new values should be updated in tblemployee.
Try with this trigger
CREATE TRIGGER dbo.tr_tblemployee
ON dbo.tblemployee
AFTER UPDATE
AS
BEGIN
IF ( UPDATE(paddr1)
OR UPDATE(paddr2)
OR UPDATE (pcity)
OR UPDATE(pstate)
OR UPDATE(pzip) )
BEGIN
INSERT INTO dbo.tblpaddresshistory
(paddr1,
paddr2,
pcity,
pstate,
pzip)
SELECT paddr1,
paddr2,
pcity,
pstate,
pzip
FROM deleted;
END
IF ( UPDATE(caddr1)
OR UPDATE(caddr2)
OR UPDATE (ccity)
OR UPDATE(cstate)
OR UPDATE(czip) )
BEGIN
INSERT INTO dbo.tblcaddresshistory
(caddr1,
caddr2,
ccity,
cstate,
czip)
SELECT caddr1,
caddr2,
ccity,
cstate,
czip
FROM deleted;
END
END;

How do I get temp values to be set after an insert has occured in a trigger?

I have a trigger I am working on that will insert rows into a table when another table has inserts or updates applied to it. So far the Update portion works (the column that I'm most concerned with is the Balance column), but when the first row is added for an insert on the Account table, in my AuditTrailCustomerBalance table OldBalance, NewBalance and CustNo are set to NULL. How can I get NewBalance and CustNo to reference to the values that were just inserted into the table from the trigger?
Here is the trigger:
ALTER TRIGGER AuditTrigger
ON Accounts
FOR INSERT, UPDATE
AS
IF UPDATE( Balance )
BEGIN
IF EXISTS
(
SELECT 'True'
FROM Inserted i
JOIN Deleted d
ON i.AccountID = d.AccountID
)
BEGIN
--1. Declare temp variables.
DECLARE #OldBalance NUMERIC( 18, 0 )
DECLARE #NewBalance NUMERIC( 18, 0 )
DECLARE #CustNo INT
--2. Set the variables.
SELECT #OldBalance = Balance FROM deleted
SELECT #NewBalance = Balance FROM inserted
SELECT #CustNo = CustNo FROM inserted
INSERT INTO AuditTrailCustomerBalance( TimeChanged, ChangedBy, OldBalance, NewBalance, CustNo )
VALUES( GETDATE(), SUSER_SNAME(), #OldBalance, #NewBalance, #CustNo )
END
END
GO
And the test statement:
INSERT INTO Custs( CustNo, GivenName, Surname, DOB, SIN )
VALUES( 1, 'Peter', 'Griffen', 'January 15, 1950', '555555555')
INSERT INTO Accounts( CustNo, Type, Balance, AccruedInt, WithdrawalCount )
VALUES( 1, 'Savings', 0, 0, 0 )
UPDATE Accounts SET Balance = 100
WHERE CustNo = 1
I believe that you want something like this:
ALTER TRIGGER AuditTrigger
ON Accounts
FOR INSERT, UPDATE
AS
INSERT INTO AuditTrailCustomerBalance(TimeChanged, ChangedBy,
OldBalance, NewBalance, CustNo )
SELECT GETDATE(), SUSER_SNAME(),
COALESCE(d.Balance,0), i.Balance, i.CustNo
FROM inserted i
left join
deleted d
on
i.AccountNo = d.AccountNo
WHERE
i.Balance <> d.Balance OR
d.Balance IS NULL
As I said in my comments, inserted and deleted can contain multiple rows (or no rows) and so you need to take that into account and write a set-based query that deals with all of those rows - also some rows may have had balance changes and some not - so deciding whether to write any entries based on UPDATE(Balance) was also flawed.
you can if you are sure of your code write something like this :
if (select count(*) from inserted) = 1
and execute your code.
You can for the insert do like this :
insert into AuditTrailCustomerBalance (.....)
select .... from inserted
as already posted, the problem with your trigger is in the calling if you update one row or multiple (same for insert)

issue with trigger in ms sql server?

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'myTrigger' AND type = 'TR')
BEGIN
DROP TRIGGER myTrigger
END
GO
go
create trigger myTrigger
on mytable_backup
instead of insert
as
begin
declare #seq int
select #seq = seq from inserted
if exists (select * from mytable_backup where seq= #seq) begin
delete from mytable_backup where seq=#seq
end
insert into mytable_backup
select * from inserted
end
go
I've written this trigger to check while inserting if seq column is repeated then update the previous row with same seq if seq doesn't exits insert it with new seq.
In ssis package I'm using OLEDB table(Mytable) as a source which contains.
Name,Age,Seq
Gauraw,30,1
Gauraw,31,1
Kiran,28,3
Kiran,29,3
kiran,28,3
Venkatesh,,4
Venkatesh,28,4
Now I'm loading this table to OLEDB destination(Mytable_backup) as destination.
I suppose to get output as.
Gauraw,31,1
kiran,28,3
Venkatesh,28,4
But I'm getting all the records from Mytable into Mytable_backup.
is anything wrong with my trigger?
I think that this trigger will just take the first row and compare it with the existing. If I understand what you want to do you can quit easy do this:
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'myTrigger' AND type = 'TR')
BEGIN
DROP TRIGGER myTrigger
END
GO
go
create trigger myTrigger
on mytable_backup
instead of insert
as
begin
insert into mytable_backup
select
*
from
inserted
WHERE NOT EXISTS
(
SELECT
NULL
FROM
mytable_backup AS mytable
WHERE
inserted.seq=mytable.seq
)
end
go
EDIT
So I found out what was going on. If you insert all of the rows in one go the inserted contains all the rows.. Sorry my mistake. If there are duplicates in your data your example do not show which to choose. I have chosen the one with the maximum of age (don't know what your requirements is). Here is a update with the full example
Table structure
CREATE TABLE mytable_backup
(
Name VARCHAR(100),
Age INT,
Seq INT
)
GO
Trigger
create trigger myTrigger
on mytable_backup
instead of insert
as
begin
;WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY inserted.Seq ORDER BY Age) AS RowNbr,
inserted.*
FROM
inserted
WHERE NOT EXISTS
(
SELECT
NULL
FROM
mytable_backup
WHERE
mytable_backup.Seq=inserted.Seq
)
)
insert into mytable_backup(Age,Name,Seq)
SELECT
CTE.Age,
CTE.Name,
cte.Seq
FROM
CTE
WHERE
CTE.RowNbr=1
end
GO
Insert of test data
INSERT INTO mytable_backup
VALUES
('Gauraw',30,1),
('Gauraw',31,1),
('Kiran',28,3),
('Kiran',29,3),
('kiran',28,3),
('Venkatesh',20,4),
('Venkatesh',28,4)
SELECT * FROM mytable_backup
Drop of the database objects
DROP TRIGGER myTrigger
DROP TABLE mytable_backup
Your original code has two flaws:
It assumes that only one record is inserted at a time.
Your insert into mytable_backup happens outside of the if condition. That insert will execute every time.