Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to know ho to limit insertion of data in database...
Posted
Comments
Sandeep Mewara 15-Jun-12 11:49am    
This is not a well framed question! We cannot work out what you are trying to do/ask from the post. Please elaborate and be specific.
Use the "Improve question" link to edit your question and provide better information.
Corporal Agarn 15-Jun-12 14:46pm    
Do you mean stop people from entering data or stop types of data from being entered?

1 solution

It sounds to me like you are looking for an Instead Of INSERT Trigger. Basically what it does is it allows you, in SQL, to look at the data before it is inserted (or update or deleted if you want) and determine if you want to allow the insertion to happen. In this way, you could limit the data going into your database.

For example, you might have a table that contained people's votes on surveys. You could set up a rule that limited people to onl one vote per day per survey. Since the person would be allowed to vote once per day, you couldn't use a UNIQUE constraint unless the Date field was the new Date type that just stored the date and not the time. The Instead Of trigger could check to see if that person already had an entry for today. If so, the change could be rejected.

A word of caution: triggers take up time in your database. The more you write, the slower your database will run. Use them wisely and only when a more efficient method is not available. For instance, the UNIQUE constraint in the above example would work for a lot of circumstances and should be tried first.

To learn more about triggers, read this article:

http://www.google.com/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=6&ved=0CG4QFjAF&url=http%3A%2F%2Fwww.codeproject.com%2FArticles%2F25600%2FTriggers-Sql-Server&ei=nHfbT72_Kqf96gGbgLW8Cw&usg=AFQjCNE2-wxIYQDD3xvXqkSGPlvJ-HSAhw[^]

Just make sure you do an Instead Of trigger instead of an After trigger. After is too late.
 
Share this answer
 
Comments
Manas Bhardwaj 15-Jun-12 17:18pm    
Well explained +5!

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