本文介绍了如何检查表中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Sno From Allotedto 1 1 150 A 2 151 249 B 3 250 500 C 4 450 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 ' 有效范围' 你可以在声明之间使用 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 DSno 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 intdeclare @to intset @from= 450set @to= 459if exists(select * from tableName where ([From]<=@from and [To] >=@from) or ([From]<=@to and [To] >=@to))print 'Invalid Range'elseprint 'Valid Range'U can use between statementIF 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'; ENDtbl_Range is the table name 这篇关于如何检查表中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!