如何进行强制数据类型检查

贝克汉鸭 2009-04-04 06:54:25
表中有一个字段A,数值类型,用查询语句
select * from table where A=1
select * from table where A='1'
都可以得出正确的结果。我想让
select * from table where A='1'
这种情况直接提示出错,SQL SERVER有类似的设置吗?
...全文
100 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcuandy 2009-04-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 beckhans 的帖子:]
表中有一个字段A,数值类型,用查询语句
select * from table where A=1
select * from table where A='1'
都可以得出正确的结果。我想让
select * from table where A='1'
这种情况直接提示出错,SQL SERVER有类似的设置吗?

[/Quote]

这是隐式转换,许可的。没有这种设置
claro 2009-04-05
  • 打赏
  • 举报
回复
Excel2007有强制数据检查功能。
claro 2009-04-05
  • 打赏
  • 举报
回复
帮顶。
没看懂。
sdhdy 2009-04-04
  • 打赏
  • 举报
回复
--字符和数字会进行隐式转换,如果转换不过去,自然会报错。
--这样,肯定会报错。
select * from table where A='a'
htl258_Tony 2009-04-04
  • 打赏
  • 举报
回复
create table tb(A int)
insert tb select 1

select * from tb where cast(A as varbinary)=cast(1 as varbinary)
/*
A
-----------
1
*/
(1 行受影响)

select * from tb where cast(A as varbinary)=cast('1' as varbinary)
/*
A
-----------

(0 行受影响)
*/
htl258_Tony 2009-04-04
  • 打赏
  • 举报
回复
create table tb(A int)
insert tb select 1

select * from tb where cast(A as varbinary)=cast(1 as varbinary)
A
-----------
1

(1 行受影响)

select * from tb where cast(A as varbinary)=cast('1' as varbinary)

A
-----------

(0 行受影响)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧