I have a problem. I created a table in Microsoft SQL and I would like one column to take not negative values. For example, EmployeeSalary column type is int and it hasn't a negative value.
3 Answers
Here is the solution of your problem:
Use CHECK while creating Table like this:
CREATE TABLE Table_name
(
col1 int CHECK (col1 >= 0)
)
OR
If you want to make a column to reject negative number, after creating the table then you can do like this:
ALTER TABLE Table_Name ADD CONSTRAINT constraint_name CHECK (col_name >= 0);
Comments
If you want to set condition when creating the table try:
EmployeeSalary money CHECK (EmployeeSalary>= 0)
If you just want to check during some script execution you can use if :
if(@Var > 0) begin
....
end
1 Comment
Panagiotis Kanavos
You can't use
IF in a query, nor should you. That's what the WHERE clause is for
CONSTRAINT.