What would prevent a trigger from seeing rows in another table? - sql

I have an AFTER INSERT trigger on this table, call it table 1.
And in the trigger I'm doing an update on another table, call it table 2.
I'm basically updating a field in table 2 from some related tables to table 1.
But for some reason the trigger can't see the row in the other table.
They are both in the same schema dbo.
What reasons would prevent the trigger from seeing the rows in the other table?
The update statement has a from clause.
But I tried just an exists clause in the trigger and it doesn't see it.
I did a sanity test with just the essentials and it worked fine.
But in the full solution it doesn't see the other table.

Related

SQL - Record Every Statement That Updated Table

I am working on a database used by separate applications. One of these applications is updating two fields in a table but I can't work out what one and don't have the source code for all the applications.
I am wondering if it is possible to write a log (to another table or elsewhere) to what the last update statement made against the table in question was. E.g. to record all SQL that has attempted to update the table automatically...
create a trigger before update on this table. Also create a new table. In that trigger store values before and after update in to a newly created table

How to create a trigger to delete a record

Looking for ideas on how to write a trigger that will delete a record when a condition is met.
I have some records being written to a SQL Server 2008 database table. These records that are being written are not important and I would like to see them removed. There are two columns that will have unique information when these records are written and I would like a trigger to remove them as soon as they are written.
I could run a daily job to delete all records that equal X and Y but my thought why not delete the records as they are written.
What would this kind of trigger look like?
If you are not able to change that at any app layer you can use an INSTEAD OF INSERT trigger and those rows will not even be inserted at all.
It will save the database from both inserting and deleting.

one sql trigger insert,update,delete for all tables in the database

is it possible to declare one Sql Trigger Insert, Update, Delete for all tables in the database instead of creating a separate trigger for each table? I just want a simple history of what actions have been taken e.g. TABLE A deletes a row ,TABLE B updates a row ,TABLE C Add new row .. and the trigger will insert a new row to another table with that information I want.
No, a trigger can only be defined on a specified table.
You should read up on the auditing features of SQL Server (http://technet.microsoft.com/en-us/library/cc280386.aspx). They are more performant and more flexible in what you want to achieve. Unfortunately they are not available in the Express Edition.

SQL Trigger - help with fundamentals

I rarely write triggers. I can use help setting up the thing.
CREATE TRIGGER audit_tableName
ON dbo.tableNameAudit
AFTER CREATE, UPDATE, DELETE on tableName
AS
BEGIN
--Get Created,updated,deletes items
INSERT into dbo.tableNameAudit(columns) VALUES ([allCUDitems])
END
GO
How do I get an iterate any CREATED, UPDATED, DELETED items that caused the trigger?
check out this msdn article.
the short of it are there are 2 special tables, inserted and deleted, that are accessible in your trigger. inserted will contain updated rows and inserted rows, while deleted will contain updated rows and deleted rows.
make sure that you understand that for a batch operation a trigger is only fired one time, so be sure to handle the possibility of multiple rows being present in those tables.
Triggers have access to two pseudo tables: INSERTED and DELETED. As their name suggests, these pseudo tables will contain all the values added or removed from the table. An update will cause a row in each of the pseudo-tables. The structure of these pseudo-tables is identical with the structure of the table on which the table is declared.
In addition the UPDATE() function inside a trigger will return TRUE for columns that were updated.
You can use INSERTED.[COLUMN] OR
SELECT [COLUMN]
FROM INSERTED
This on SQL Server.
Hope it helps.

DDL statements against deleted inserted in DML trigger

I am trying to find impact of doing DDL statement against deleted and inserted logical tables inside table trigger. I have:
CREATE TRIGGER [Trigger52]
ON [dbo].[Table1]
FOR DELETE, INSERT, UPDATE
AS
BEGIN
create table inserted (c1 int)
select * from inserted
END
When it is triggered, I expected to get an error. Instead, it seems to ignore create table statement entirely and select rows that have been inserted.
Is there a documentation describing this behavior or explanation?
Inside triggers there are always two pseudo-tables existing: inserted and deleted. See Using the inserted and deleted Tables:
SQL Server automatically creates and
manages these tables. You can use
these temporary, memory-resident
tables to test the effects of certain
data modifications and to set
conditions for DML trigger actions.
You cannot directly modify the data in
the tables or perform data definition
language (DDL) operations on the
tables.