Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Create Table with Check condition

Author  Topic 

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-17 : 05:02:55
I want to create a table, when insert a record it wont allow 0 or lessthan 0.

How do i check particular column

For Ex:

Create Table Table1
(
Id INT,
Rate Number(18,6)
)

I wont allow Rs.0 for Rate Column

kindly reply

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-01-17 : 05:15:32
Use Check constraint while creating table like given below:

Create Table #Table1
(
Id INT,
[Rate Number] numeric(18,6) check ([Rate Number] > 0)
)

This will throw exception "The INSERT statement conflicted with the CHECK constraint" if value 0 or less than 0 is entered.
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-17 : 05:23:34
Thanks for your reply, Is it possible to handle the exception here
Go to Top of Page

sunil
Constraint Violating Yak Guru

282 Posts

Posted - 2008-01-17 : 05:38:24
It will be better if you check in front end before inserting values into database. In that case, check option won't be needed. Its better to validate in front end rather in backend. For exception, it can be handled using Try catch when insertig values into database and then catching message in front end. Search BOL for try catch. My advice would be make check in front end.
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-17 : 05:43:56
Ok thank you
Go to Top of Page
   

- Advertisement -