Creating A Trigger When Insert Is Already Done - sql

I created a SQL trigger where I want to update a column("Documents") in my table "Article". I have another table which is "Files" where a FileId is stored. So now i want that FileId in my column "Documents".
I created this trigger:
CREATE TRIGGER [InsertDocumentId]
ON [dbo].[Files]
FOR UPDATE
AS
INSERT INTO [Article]
(Documents)
SELECT
FileId FROM Files LEFT JOIN Article t on Files.ArticleId = t.ArticleId;
GO
But this isn't working...
Hope you guys can help me out.

If I understand correctly, you want to insert table B after insert table A. If that's correct, you can try to use this block of query:
CREATE TRIGGER [InsertDocumentId] ON [dbo].[Files]
FOR INSERT
AS
INSERT INTO [Article] (Documents)
SELECT FileId FROM INSERTED f;
GO

Related

How to create trigger to check if table changed

I'm beginner and i trying to create trigger to check if table changed then i track the changed and insert it into another table
i created table called history and this is my code
create table history
(
ProjectNo INT ,
UserName NVARCHAR(50),
ModifiedDate date,
Budget_Old int,
Budget_New int,
)
and created this trigger
CREATE TRIGGER t1 on history
on project AFTER UPDATE
AS BEGIN
IF UPDATE(Project.Pnumber,Project.Budget)
INSERT INTO dbo.history (ProjectNo , Username,ModifiedDate,Budget_Old,Budget_New)
SELECT
d.ProjectNo, suser_name() ,GETDATE(),d.Budget,i.budget
FROM
Deleted d , inserted i
where d.projectno = i.projectno
END
i think my if statment is wrong but what i should do to make my query run right
to insert this values in history table ? plz help me and sorry for bad English
Triggers have access to two logical tables that have an identical structure to the table they are defined on which us Project as I assume.
INSERTED, which is the new data to go into the table
DELETED, which is the old data the is in the table
So You can get the old values in this way:
CREATE TRIGGER t1
ON dbo.Project AFTER UPDATE
AS
INSERT INTO dbo.history (ProjectNo , Username)
SELECT d.ProjectNo , d.Username
FROM DELETED d
If you need to just have one record for specific project in the table history, then you would use inner join based on the projectNo and update the history table accordingly.
ITS WORKS BUDDY ENJOY!!!!!!!!!!!
CREATE TRIGGER tRIGGERNAME1
BEFORE UPDATE on history
FOR EACH ROW
BEGIN
INSERT INTO dbo.history (ProjectNo ,Budget_Old,Budget_New)
VALUES(:OLD.ProjectNo,:OLD.Budget_Old,:NEW.Budget_New);
END;

trigger after insertion method

I am trying to built trigger in which when user delete a row after deleted user will able to view the row which he deleted.
i have tried my coading is follows
please help.
create trigger insertion
before insert on client
for each row
as
select * from client
Try this:
create trigger deltrig
on client
for delete
as
select deleted
delete from [<YourTable>] output deleted.*
/* where ... */
isnt something like this what you want?
create table #deleted(nr int)
create table #teste_d(nr int)
insert into #teste_d
values(1),(2),(3)
delete from #teste_d
OUTPUT deleted.nr INTO #deleted
where nr = 2
select * from #deleted
drop table #deleted
drop table #teste_d
the resultset would be what was deleted
or if you really want to use trigger
should be instead delete or update
you should do something like
insert into ##Data_deleted (col1,col2,...)
select col1,col2,... from deleted

SQL Trigger to update a field on insert if the inserted value conflicts with the unique constraint

I have a Table called Albums which contains a field IsDeleted and a field "Name".
Name is a unique key. My Question is:
Can I create a trigger which updates IsDeleted to False if it is set to True and the same name is inserted into the table by the user?
Of course the insert statement should somehow be changed to an update statement.
I'm Using MSSQL 2008 and I'm very new to triggers and SQL ingeneral
Thanks for your help!
Yes, you can do this with an instead of trigger. Example:
create trigger albums_ins_trig on albums instead of insert as begin
-- update albums which already exist and are being inserted again
update albums set IsDeleted=0 where IsDeleted=1
and exists (select * from inserted where albums.name=inserted.name)
-- insert new albums
insert into albums select * from inserted
where not exists (select * from albums where albums.id=inserted.id)
end

How to Use FIRE_TRIGGERS in insert sql statement

I am trying to copy data from table "tb_A" to itself (with different primary key).
When "tb_A" table is insert new record, I have written a trigger to populate another table "tb_B" with one record.
I ran the following statement.
INSERT INTO [tb_A]
([NAME])
select top (20)[NAME] from [tb_A]
I was expected 20 new records in "tb_B". But I didn't.
Anyway I saw FIRE_TRIGGERS is using during bulk insert to overcome this issue.
is there is a any way to use it on inset statements too ? Please provide me example.
Gayan
Trigger code (copied from Gayan's comment to gbn's answer):
CREATE TRIGGER UpdatetbB ON [dbo].[tb_A] FOR INSERT
AS
DECLARE #AID as int
SELECT #AID = [ID] FROM inserted
INSERT INTO [tb_B]([IDA]) VALUES (#AID)
The reason your trigger did not work properly is because it is poorly designed. Triggers fire once for each insert even if you are inserting a million records. You havea trigger that makes the assumption it will owrk one record at a time. Anytime you set a value form inserted or deleted to a scalar variable the trigger is wrong and needs to be rewritten. Try something like this instead.
CREATE TRIGGER UpdatetbB ON [dbo].[tb_A] FOR INSERT
AS
INSERT INTO [tb_B]([IDA])
SELECT [ID] FROM inserted
FIRE_TRIGGERS is only for BULK INSERT (and bcp), not "standard" INSERT
I'd expect your trigger to look something like
CREATE TRIGGER TRG_tbA_I ON tb_A FOR INSERT
AS
SET NOCOUNT ON
INSERT tb_B (col1, col2, ...)
SELECT col1, col2, ... FROM INSERTED
GO
You use the special INSERTED table to get the list of new rows in tb_A, then INSERT from this into tb_B. This works for more than one row
If you add the trigger code then we can explain what went wrong.
Edit: your trigger will only read a single row (any row, no particular order) from INSERTED. It isn't set based like my rough example.

SQL Server 2005 triggers

I created a trigger on a table for updates. If any update happens, I want to store the old value in separate table. I am trying to get the old value from "inserted" table, but this table is not populated with old values after update.
Here is the sample code:
CREATE TRIGGER [dbo].[Logs_Update]
ON [dbo].[Logs] AFTER UPDATE
AS
DECLARE #url varchar(50)
BEGIN
SELECT #url = i.url
FROM INSERTED i
INSERT INTO dbo.Triggers_tbl
(ID, URL)
VALUES
(1000, #url)
END
I get #url as null from the inserted table.
Please let me know what is wrong with the trigger
The DELETED table contains the "old" values and the "INSERTED" table contains the "new" values.
To add to this, the INSERTED and DELETED tables may contain multiple rows if your update affects multiple rows and therefore you should probably run your insert with an INSERT SELECT rather than a variable.
INSERT INTO dbo.Triggers_tbl(URL) SELECT d.url FROM DELETED d
The "old" values (after an UPDATE) are available in the Deleted pseudo-table:
CREATE TRIGGER [dbo].[Logs_Update]
ON [dbo].[Logs]
AFTER UPDATE
AS
DECLARE #url varchar(50)
SELECT #url = d.url from deleted d
INSERT INTO dbo.Triggers_tbl(ID,URL) VALUES(1000,#url)
As HLGEM correctly commented on, the OP's code is assuming the trigger will be called for each row separately - which is not correct.
In that light, the trigger code really ought to deal with a set of rows being updated, and thus it should be something like:
CREATE TRIGGER [dbo].[Logs_Update]
ON [dbo].[Logs]
AFTER UPDATE
AS
INSERT INTO dbo.Triggers_tbl(ID,URL)
SELECT 1000, d.url
FROM Deleted d
or something like that.
The records that were updated are in the DELETED virtual table, not INSERTED. Change "from inserted" to "from deleted" and that should work.