Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to develop a trigger for SQL. The trigger should get triggered when i save,update or delete any record in a particular database.

Or

I want to fill my data again only if data has been saved,updated or deleted.

Is this possible? If yes could you provide with a sample code? Thanks in advance.
Posted
Updated 16-Jan-14 1:31am
v2
Comments
Sandeep Singh Shekhawat 16-Jan-14 7:21am    
Triggers are defined on table so it will fire on DML event on table.
agent_kruger 16-Jan-14 7:22am    
sorry but i have no knowledge what is "DML event" can you ellaborate on the topic?
Ankur\m/ 16-Jan-14 22:58pm    
Can you not "Google" on the topic?
agent_kruger 17-Jan-14 5:32am    
i tried but was unable to find a good article

Here is a good article on triggers
 
Share this answer
 
v2
Comments
agent_kruger 16-Jan-14 7:24am    
how to receive that save,update or delete is performed on the table. Can you provide a code snippet?
Hammad 16-Jan-14 7:26am    
can't understand your question can you explain it,
He seems to be asking for a trigger that fires to his win form. It's possible to write C# code that you can call from SQL, but you'd need that code to call something external to provide some sort of event. It's not at all trivial, and I don't recommend it. In fact, as the web would need to be the common layer, having your code HOST a web service is the only way I can think to make this work.

Triggers fire inside your DB, your DB does not care if you're calling it from C#, from C++ or from Applesoft basic. As such, it has no mechanisms for external events.

The thing to do is simply to poll your DB regularly to see if it's changed.
 
Share this answer
 
Comments
agent_kruger 17-Jan-14 5:35am    
you are right sir, but i don't want to fire query again and again to check for change instead after every change database receive should send me some kind of information. Is that possible?
Christian Graus 18-Jan-14 0:00am    
Yes. I know the answer, but I was trying to keep it a secret from you, but you wore me down.

Read my answer again. No, it's not possible. Your DB does not know how to talk to the outside world, unless you can do something via .NET code, which will certainly slow it down. Imagine having a lot of data coming in, and each new row has the added cost of looking up and calling a web service ( which, as I said, is the only real way of doing it that I can see unless the server is on the same network as the app being used )
agent_kruger 18-Jan-14 4:32am    
so what does a trigger actually do can you brief me about that i cannot understand the article.
Christian Graus 18-Jan-14 4:39am    
A trigger fires inside the database. It allows you to inject code INSIDE the database on an event. It does the sort of thing you're asking for, but only inside the database. It cannot talk to the world outside the database.
agent_kruger 18-Jan-14 4:51am    
ok, thank you sir for your precious time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900