SQL Server - trigger AFTER INSERT table in schema - sql-server

I have a stored procedure to run after a set of tables, all belonging to the same schema [DATA_Countries], is dropped and then re-inserted.
The operation is performed by another application, which drops and recreates the target table, over which I have no control.
Since the table is dropped and recreated each time, I can not use triggers on each target table.
Is there a way to get a trigger for each time a table is inserted into a specific schema, to return the name of such table and launch a parametrized stored procedure?
Thanks!

Yes, You can create DDL Triggers on SQL Server to track the DDL Changes. For example, If I want to track the changes in Stored Procedures on my Database AdventureWorks, I can create a trigger like this
CREATE TRIGGER td_ProcTrack
ON AdventureWorks
FOR CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE
AS
BEGIN
<my code>
END
Refer this Article for more detailed examples

Related

what is the purpose of database level trigger in the DDL trigger?

I am confused that what is the on database in the below trigger what is the purpose of using that please let me know what is the use of that
create trigger trmyfirsttrigger
on database
for create_table,alter_table,drop_table
as
begin
rollback
print 'you can not create ,alter,drop table.'
end
The trigger is defined at the database level and is intended to prevent any create, alter and drop statements on any table. That means you cannot create a new table, alter or delete existing tables on the database. This is generally used by DBAs to lock the database from any changes during maintenance & patching.

SQL Server update query triggered by stored procedure

Is there a way to have a SQL Server stored procedure trigger a query without editing the stored procedure code?
I am looking for a way to do something along the lines of a CREATE TRIGGER, but have it be triggered by a stored procedure instead of table update without altering the original code for the stored procedure.
I would create a trigger for when the table updates, but the stored procedure that updates the table updates it tens of thousands of times, so that is unfeasible.
select * from sys.dm_exec_procedure_stats
I would check the "execution_count" to the system table listed above. You can create a stored procedure and then create a job that runs that stored procedure at a specified interval.

Alter table to disable trigger in stored procedure

If we have an alter table statement within a stored procedure. Will it just affect that session or will it affect all sessions?
We use the alter table to disable the triggers that occur because we don't want them run when the stored procedure occurs but want the update triggers to run at all other times.
Cheers,
"Alter table" is an DDS operation - it will change the DB structure for all connections.
Here is a trick: create temp table with unique name like #no_triggers_for_[procname] at the beginning of your sp and check for it's existence within triggers.

Concurrency problems with temp tables in consequent batches?

I have sometimes a problem when running a script. I have the probelm when using an application (that I didn't write and therefore cannot debug) that launches the scripts. This app isn't returning the full error from SQL Server, but just the error description, so I don't know exactly where th error comes.
I have the error only using this tool (it is a tool that sends the queries directly to SQL Server, using a DAC component), if I run the query manuallyin management studio I don't have the error. (This error moreover occurs only on a particular database).
My query is something like:
SELECT * INTO #TEMP_TABLE
FROM ANOTHER_TABLE
GO
--some other commands here
GO
INSERT INTO SOME_OTHER_TABLE(FIELD1,FIELD2)
SELECT FIELDA, FIELDB
FROM #TEMP_TABLE
GO
DROP TABLE #TEMP_TABLE
GO
The error I get is #TEMP_TABLE is not a valid object
So somehow i suspect that the DROP statement is executed before the INSERT statement.
But AFAIK when a GO is there the next statement is not executed until the previous has been completed.
Now I suspoect that this is not true with temp tables... Or do you have another ideas?
Your problem is most likely caused by either an end of session prior to the DROP TABLE causing SQL Server to automatically drop the table or the DROP TABLE is being executed in a different session than the other code (that created and used the temporary table) causing the table not to be visible.
I am assuming that stored procedures are not involved here, because it looks like you are just executing batches, since local temporary tables are also dropped when a stored proc is exited.
There is a good description of local temporary table behavior in this article on Temporary Tables in SQL Server:
You get housekeeping with Local Temporary tables; they are
automatically dropped when they go out of scope, unless explicitly
dropped by using DROP TABLE. Their scope is more generous than a table
Variable so you don't have problems referencing them within batches or
in dynamic SQL. Local temporary tables are dropped automatically at
the end of the current session or procedure. Dropping it at the end of
the procedure that created it can cause head-scratching: a local
temporary table that is created within a stored procedure or session
is dropped when it is finished so it cannot be referenced by the
process that called the stored procedure that created the table. It
can, however, be referenced by any nested stored procedures executed
by the stored procedure that created the table. If the nested
procedure references a temporary table and two temporary tables with
the same name exist at that time, which table is the query is resolved
against?
I would start up SQL Profiler and verify if your tool uses one connection to execute all batches, or if it disconnects/reconnects. Also it could be using a connection pool.
Anyway, executing SQL batches from a file is so simple that you might develop your own tool very quickly and be better off.

trigger in sql server 2005

I want to create trigger for one table for insertion...If i insert records in one table then that same records should be inserted to another one......how?
and also explain about triggers
All you need to know about triggers, without having to wade through Microsoft speak, including how to create them in SQL Server.
http://en.wikipedia.org/wiki/Database_trigger
It should be something like
CREATE TRIGGER TableInserted ON TableInsert
AFTER INSERT
AS
INSERT INTO OtherTable SELECT * FROM inserted
This article covers all the basics of creating triggers.
Though for you task i think a stored procedure doing both inserts will be better, because what if one insert succeeded and the other failed? it will create inconsistency in your data. See here for stored procedures.

Resources