views:

28

answers:

1

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?

+1  A: 

If i understand you correctly, this might help you

DECLARE @Table TABLE(
     FromVal1 FLOAT,
     ToVal1 FLOAT,
     FromVal2 FLOAT,
     ToVal2 FLOAT,
     FromVal3 FLOAT,
     ToVal3 FLOAT,
     FromVal4 FLOAT,
     ToVal4 FLOAT
)

INSERT INTO @Table (FromVal1,ToVal1,FromVal2,ToVal2) SELECT 1, 10, 51, 60
INSERT INTO @Table (FromVal2,ToVal2) SELECT 11, 20
INSERT INTO @Table (FromVal3,ToVal3) SELECT 21, 30
INSERT INTO @Table (FromVal4,ToVal4) SELECT 31, 40
INSERT INTO @Table (FromVal1,ToVal1) SELECT 41, 50

DECLARE @FromVal FLOAT,
     @ToVal FLOAT

SELECT  @FromVal = 25,
     @ToVal = 35

SELECT  *
FROM    @Table
WHERE   NOT(FromVal1 > @ToVal OR ToVal1 < @FromVal)
OR   NOT(FromVal2 > @ToVal OR ToVal2 < @FromVal)
OR   NOT(FromVal3 > @ToVal OR ToVal3 < @FromVal)
OR   NOT(FromVal4 > @ToVal OR ToVal4 < @FromVal)
astander
Off course, create a view, so that all four spans are in one column... I knew I am missing something :) Thanks!
_simon_