在sql server中,如果我们使用了image,text和ntext类型来存储数据的时候,如何来判断该字段是否含有数据值呢?在oracle中,我们可以通过设定blob类型的变量来直接查询,并判断其内容是否为NULL,但是在sql server中,我们不能通过直接设定image、text和ntext类型的变量来查询,然后再比较,因为这三种类型是不是可用的变量类型,找了很多方法,但是凡是涉及到字符串的函数都无能为力,最后找了一个一个系统函数DATALENGTH ( expression ),该函数直接获取表达式所占用的字节数,而且对上述三种类型都可以进行计算,于是问题解决了。
看一个例子:
CREATE PROCEDURE [dbo].[sp_insertUpdateHotelIntroduction]
@hotelID int,
@introduction ntext ,
@introductionLenght int = 0,
@pictureLen int =0,
@pic image
AS
BEGIN
SET NOCOUNT ON;
select @introductionLenght = DATALENGTH(TxtIntroduction) ,@pictureLen =DATALENGTH(picture) from hotels where ID = @hotelID
if ((@introductionLenght is null) and DATALENGTH(@introduction)>0)
begin
update hotels set TxtIntroduction = @introduction where ID = @hotelID
end
if ((@pictureLen is null) and DATALENGTH(@pic)>0)
begin
update hotels set picture=@pic where ID = @hotelID
end
begin tran mytran
declare @text_NeedInsert binary(16) -- 定义指针
Select @text_NeedInsert = TEXTPTR(TxtIntroduction) from Hotels with(nolock) where id = @hotelID --设置指针
if @text_NeedInsert is not null
begin