日期:2014-05-18  浏览次数:20488 次

求助:如何查询在一个表的两个字段之单的数据


如图aa表中的两个字段
如何查询002在001和003之间
如何查询005在004和006之间
我要的结果是当我插入002的时候提示该数据已经存在了,只求sql语句。大侠们帮帮忙,谢谢!

------解决方案--------------------
SQL code
declare @i varchar(10)='002'
if exists(select 1 from tb where beginno<@i and endno>@i)
print 
'@i 已经存在

------解决方案--------------------
SQL code
declare @a varchar(3)
set @a='002'
if exists (select 1 from tb where @a >beginno and @a<endno)
print '该数据已经存在了'
else
insert into tb ......