0

I am not sure, if this can be achieved in SQL.

INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, 999); 

If the user tries to insert 999 in column3, convert it to null or avoid inserting it.

I am trying to find if I can find any solution from SQL Server side.

2 Answers 2

1

How about using a Check Constraint:

ALTER TABLE table_name ADD CHECK (column3<>999); 
Sign up to request clarification or add additional context in comments.

2 Comments

instead of terminating the statement, can we convert to null- @Mark Kram
You would have to use instead of triggers to convert 999 to a NULL. You would need both insert and update triggers to accomplish this. I would not recommend this approach though as I strongly dislike the system changing data that is being inserted.
0

You cannot case the 999 in the insert statement. It really depends on the implementation. Is this strictly in the Database? Is it being called with a java/python/scala library? In the database, I would recommend the following:

set @value1 = val1; set @value2 = val2; set @value3 = if(val3=999,NULL,val3); INSERT INTO table_name (column1, column2, column3) VALUES (@value1, @value2, @value3); 

If outside the Database, just use the same logic.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.