sql检测有无某条记录

[复制链接]
查看11 | 回复4 | 2009-1-15 11:02:26 | 显示全部楼层 |阅读模式
目的:检测某条记录是否存在,比如下面的命令为检测是否有id=1972的记录存在,结果不存在,那么请问此时读取该表中字段name=rs("name")的值为什么呢?
select* from product where id=1972
if rs("id")null then
response.write("ok")
else
response.write("no")
end if
可是为什么这几句代码执行不成功呢 ?
当查询的记录存在的时候能显示ok,当记录不存在的时候也不显示no,这个是为什么呢?

回复

使用道具 举报

千问 | 2009-1-15 11:02:26 | 显示全部楼层
select case when exists(select 1 from table where id=1972) then t.name else '' end name from table t where t.id=1972----------------------------从你的问题上看 你的id跟name字段是没有关联的------------------------但从你补充的问题上来看 你是关联的在你做if rs("id")null then response.write("ok") else response.write("no") end if 判断之前 请先判断rs存...
回复

使用道具 举报

千问 | 2009-1-15 11:02:26 | 显示全部楼层
很明显为空此条数据不存在 当然其字段也不存在...
回复

使用道具 举报

千问 | 2009-1-15 11:02:26 | 显示全部楼层
本身不存在如何来的纪录判断 rs是否为null不就可以了或者 if rs.eof thenresponse.write ("no")elseresponse.write ("ok")end if...
回复

使用道具 举报

千问 | 2009-1-15 11:02:26 | 显示全部楼层
select count(*) as count from product where id=1972if rs("count ")>0 then response.write("ok") else response.write("no") end if 即可判断...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行