日期:2014-05-19  浏览次数:20526 次

如何判断已存在数值?
在数据库中有一个Ca_ID字段,已经存储了“1,2,3,4”4个值,现在添加数据,如果添加重复数据则返回一个值非0值。然后在代码中抛出异常。

请问这个sql语句怎么写?谢谢!

------解决方案--------------------
declare @Ca_ID int
set @Ca_ID=...

if exists(select 1 from 表 where Ca_ID=@Ca_ID)
return -1
else
insert into 表(Ca_ID) values(@Ca_ID)
------解决方案--------------------
if exists(select 1 from tbName where charindex(ID, Ca_ID)> 0)
print '存在 '
else
print '不存在 '