G. Soós
G. Soós

Reputation: 47

Microsoft SQL int type take not negative value

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.

Upvotes: 2

Views: 15028

Answers (3)

Dan
Dan

Reputation: 1

go tools>options>designer

"prevent changes that require table re-creation" this one should be unchecked.

Upvotes: 0

Nikolay Minev
Nikolay Minev

Reputation: 115

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

Upvotes: 0

Nishant Gupta
Nishant Gupta

Reputation: 3656

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);

Upvotes: 7

Related Questions