如何检查表中是否存在值 [英] How to check whether values exist in a table

查看:77
本文介绍了如何检查表中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Sno From Allotedto 
1 1 < span class =code-digit> 150 A
2 151 < span class =code-digit> 249 B
3 250 < span class =code-digit> 500 C
4 450 < span class =code-digit> 459 D



在上下文中不允许使用Sno 4,因为值为450 t0 459在sno 3范围内。如何检查sql server中的这些条件。

解决方案

试试这个..



 声明 @from int 
声明 @to int

set @ from = 450
set @ to = 459


如果存在(从tableName中选择 *其中([From]< = @ from [ ]> = @ from)([From]< = @ to [ To ]> = @ to))
print ' 无效范围
else
print ' 有效范围'


你可以在声明之间使用

< br $>


  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 ' 不重复';
END





tbl_Range是表名


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.

解决方案

Try this..

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'


U can use between statement


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


这篇关于如何检查表中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆