您的位置:首页 > 数据库

sql 查询条件字段为text或ntext 的解决方案

2010-03-12 10:36 405 查看
sql 查询条件字段为text或ntext得解决方案以及varchar(max)、nvarchar(max)
1、在MS SQL2005及以上的版本中,加入大值数据类型(varchar(max)、nvarchar(max)、varbinary(max) )。大值数据类型最多可以存储2^30-1个字节的数据。

这几个数据类型在行为上和较小的数据类型 varchar、nvarchar 和 varbinary 相同。

微软的说法是用这个数据类型来代替之前的text、ntext 和 image 数据类型,它们之间的对应关系为:

varchar(max)-------text;

nvarchar(max)-----ntext;

varbinary(max)----image.

有了大值数据类型之后,在对大值数据操作的时候要比以前灵活的多了。比如:之前text是不能用‘like’的,有了varchar(max)之后就没有这些问题了,因为varchar(max)在行为上和varchar(n)上相同,所以,可以用在varcahr的都可以用在varchar(max)上。

所以请使用 varchar(max)、nvarchar(max) 和 varbinary(max) 数据类型,而不要使用 text、ntext 和 image 数据类型。
2、如果需要处理已经存在的text类型 的查询 则需要进行字段转换下 where cast(text as varchar)

3、实例如下,其中i_item为ntext

select * from IP_Investigate
where I_InvestigateID in (select IS_ID from IP_InvestigateStatus
where IS_Name like 'tomcat%') and cast(i_item as varchar)='1'

update IP_Investigate
set i_order=5
where I_InvestigateID in (select IS_ID from IP_InvestigateStatus
where IS_Name like 'tomcat%') and cast(i_item as varchar)='5'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: