Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i insert rows into a table, particular column could not allow particular word or Amount.

Example,i have a table called Student_Master(Name, Age,Department), where a Age must have greater than 18 to be included in the table Otherwise it could not insert..

How can i write this in insert queries..

Thanks in Advance......
Posted
Updated 11-Jun-13 6:17am
v2
Comments
Richard C Bishop 11-Jun-13 11:23am    
So just add an expression to your where clause that checks the age. What is the problem?
gvprabu 11-Jun-13 11:36am    
I think U need to Check before Insert right
CHill60 11-Jun-13 11:37am    
Where are you getting the data from to insert? What does your insert statement currently look like?
gvprabu 11-Jun-13 11:39am    
See Y Department master table having Age, Name ? simply Don't ask like this question in Code Project and don't waste out time.
gvprabu 11-Jun-13 11:40am    
before insert, U have to create TRIGGER and check the values or CHECK Constraint you have to create for your table then u have to check the values.

1 solution

It's not a good idea to do this as part of your INSERT query - it's too easy to have it repeated in multiple places in your code and make a mistake, or miss one if the cut-off age changes to 21 say.

Instead, this would probably be best handled by using a stored procedure on the database to check or insert:
SQL
CREATE PROCEDURE InsertWithAgeCheck 
	@PName VARCHAR(100),
	@Age int,
	@Dept VARCHAR(100) 
AS
BEGIN
IF (@Age < 18) 
   RAISERROR('Too Young',18, 1)
ELSE
   INSERT INTO MyTable ([Name], [Dept], [Age]) VALUES (@PName, @Dept, @Age)
END
GO
 
Share this answer
 

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