一个SQL自定义函数的问题~
ALTER FUNCTION dbo.getIsOnline
()
RETURNS nvarchar(100) AS
begin
declare @IsOnLine as nvarchar(100)
declare @OnLineTime as int
set @OnLineTime = 0
set @IsOnLine = ' '
select @OnLineTime = OnLineTime from List
if(@OnLineTime < 60)
begin
set @IsOnLine = '是 '
end
if(@OnLineTime > = 60)
begin
set @IsOnLine = '否 '
end
return @IsOnLine
end
上面是我自己写的函数
select @OnLineTime = OnLineTime from List
中的OnLineTime是一个整数字段它是由datediff来获得的list是视图
在执行的时候发现IF语句没有其效果所有的IsOnLine的值都是 "否 "
为什么会这样,急死人了。大家帮下忙好么
------解决方案--------------------ALTER FUNCTION dbo.getIsOnline
(@id varchar(100))
RETURNS nvarchar(100) AS
begin
declare @IsOnLine as nvarchar(100)
declare @OnLineTime as int
set @OnLineTime = 0
set @IsOnLine = ' '
select @OnLineTime = OnLineTime from List where id = @id --- 在这个地方加一个条件就好了
if(@OnLineTime < 60)
begin
set @IsOnLine = '是 '
end
if(@OnLineTime > = 60)
begin
set @IsOnLine = '否 '
end
return @IsOnLine
end
------解决方案--------------------你的list視圖中肯定會很我記錄,select @OnLineTime = OnLineTime from List這條語句會將敢后一條記錄返回給@OnLineTime,也就是@OnLineTime得到是最后一條記錄的值