I hope you can understand my question, if not, please let me know...
In my form I have eight textboxes, like so (user wrote '25' to first textbox and '35' to second one):
Code1From: _25____ Code1To:_35____
Code2From: _______ Code2To:_______
Code3From: _______ Code3To:_______
Code4From: _______ Code4To:_______
My table looks like this:
create table MyTable (
Id int identity(1,1),
Code1From bigint, Code1To bigint,
Code2From bigint, Code2To bigint,
Code3From bigint, Code3To bigint,
Code4From bigint, Code4To bigint
)
Now I'd like to prevent inserting data, that is allready inserted. For example: Data in MyTable:
Id, Code1From, Code1To, Code2From, Code2To, Code3From, Code3To, Code4From, Code4To
1, 1, 10, null, null, null, null, null, null
2, 11, 20, null, null, null, null, null, null
3, 21, 30, null, null, null, null, null, null
4, 31, 40, null, null, null, null, null, null
5, 41, 50, null, null, null, null, null, null
If user wants to insert for Code1 (or Code2, Code3 or Code4) from 25 to 35, I should raise an error (because span from 25 to 35 is allready in the database - id 3 and 4). However, user can insert span from 51 to 55 for example.
How can I determine, if a span is allready in my database? Now I could do something like this:
select *
from MyTable
where
@code1From between Code1From and Code1To
or @code1From between Code2From and Code2To
or @code1From between Code3From and Code3To
or @code1From between Code4From and Code4To
--
or @code1To between Code1From and Code1To
or @code1To between Code2From and Code2To
or @code1To between Code3From and Code3To
or @code1To between Code4From and Code4To
--
... and another 24 or statements
Is there any easier way to accomplish this?