SQLTeam.com | Weblogs | Forums

Run a Script each time a table is modified


#1

Hi,

I need to find the way to run a batch script (made by me) each time a single table in my Microsoft SQL Server is modified.

Kind of every time a field is updated or a new registry is created...

Been looking for a while but couldn't find anything. I hope you can help me!

Sandritto


#2

You might want to have a look at triggers


#3

If I understand you correctly, you need to look at DDL triggers.

I don't understand what you mean by "a new registry is created", but I think you mean a new table or perhaps view or some other object.


#4

I will! By a new registry I mean a new row. Direct translated from Spanish sorry.


#5

You will need to create insert triggers, or do all inserts through stored procedures, that execute your batch code for each table you want to trigger execution. Know this will slow down inserts. What are you trying to accomplish with your batch?


#6

I only need it for one table. It is not a very used table, maybe 4 or 5 inserts and 4 or 5 updates a day.

The batch script will get the first field of the row that has been updated or inserted and use it as a variable in the script.


#7

How many rows are in the table?


#8

Create a job to run this, add a step as OS command (cmdexec)

bcp schema.table out -T -S server -d database-c -o \\path\table.txt

replace schema.table, server, database, path with your values.


#9

The table has around 5.000 rows.
I will try your idea during this week and I'll let you know the result!
Thanks.