Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Sno       From          To     Allotedto
 1         1            150       A
 2         151          249       B
 3         250          500       C
 4         450          459       D

Sno 4 should not allowed in the context because the values 450 t0 459 is present in sno 3 range.How to check these condtion in sql server.
Posted
Updated 25-Jul-13 2:02am
v3
Comments
vinayoukuri 25-Jul-13 8:09am    
Do you like to select such records or you want to stop inserting such records into table.
Bhagavan Raju M 25-Jul-13 8:12am    
@vinayoukuri I want to stop inserting records into table
Maciej Los 25-Jul-13 8:26am    
I'm sure that i had posted answer for similar question...

Try this..

VB
declare @from int
declare @to int

set @from= 450
set @to= 459


if exists(select * from tableName where ([From]<=@from and [To] >=@from) or ([From]<=@to and [To] >=@to))
print 'Invalid Range'
else
print 'Valid Range'
 
Share this answer
 
v2
U can use between statement


SQL
IF EXISTS( SELECT 1 FROM tbl_Range t WHERE  @To BETWEEN t.[From] AND t.[To]
                                              AND  @From BETWEEN t.[From] AND t.[To])
  BEGIN
    print 'duplicate';
  END
 ELSE
  BEGIN
    print 'Not duplicate';
  END


tbl_Range is the table name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900