在数据库里面写的函数报错,找不出什么原因,谢谢大虾给看看啊,小女子哭求(100)

  • 主题发起人 主题发起人 幼儿
  • 开始时间 开始时间

幼儿

Unregistered / Unconfirmed
GUEST, unregistred user!
在数据库里面写的函数报错,找不出什么原因,谢谢大虾给看看啊,小女子哭求:这个是创建的数据库表:CREATE TABLE [SchemeDataTab] ( [SchDataID] [int] NOT NULL , [CaseID] [int] NULL , [SchID] [int] NULL , [FstGlucose] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [OldGlucose] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [CurGlucose] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [CurInsulin] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [Feeding] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [OldGlucoseTime] [datetime] NULL , [NewInsulinIn] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [MindSufferer] [int] NULL , [SugarDiabetes] [int] NULL , [Incretion] [int] NULL , [InsulinDose] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [GlucoseDose] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [NewInsulinOut] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [NewGlucoseTime] [datetime] NULL , [CallDoctor] [int] NULL CONSTRAINT [DF_SchemeDataTab_CallDoctor] DEFAULT (0), [InsulinDoseFact] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [GlucoseDoseFact] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [NewInsulinOutFact] [char] (10) COLLATE Chinese_PRC_CI_AS NULL , [NewGlucoseTimeFact] [datetime] NULL , [CallDoctorFact] [int] NULL , [crtDate] [datetime] NULL , CONSTRAINT [PK_SchemeDataTab] PRIMARY KEY CLUSTERED ( [SchDataID] ) ON [PRIMARY] ) ON [PRIMARY]GO这个是在这个表的基础上的函数:CREATE FUNCTION xh_LowTimeYSFW (@LowGlucose char(10),@curtime datetime,@Caseid int,@SchID int)RETURNS char(20)AS BEGIN DECLARE @d_retun char(20) SET @curtime = CAST(@curtime as datetime) begin set select @d_retun = (DATEDIFF(hour,OldGlucoseTime,crtDate)*(@LowGlucose-GlucoseDoseFact)+ DATEDIFF(hour,crtDate,NewGlucoseTimeFact)*(CurGlucose-@LowGlucose))/(CurGlucose-GlucoseDoseFact) from (select top 1 OldGlucoseTime,CurGlucose,GlucoseDoseFact,NewGlucoseTimeFact,OldGlucoseTime,crtDate from SchemeDataTab where CurGlucose>@LowGlucose and GlucoseDoseFact<@LowGlucose and Caseid=@Caseid and SchID=@SchID and DATEDIFF(hour,OldGlucoseTime,NewGlucoseTimeFact)<24) end return @d_retunEND
 
现在上大富翁的大虾越来越少了吧,唉~~~
 
小女子..Char和Char相减?还相除?如果你是想做浮点数运算,先用转换函数
 
可以的啊,之前的一个函数可以的,我想应该不是这里的问题,虽然是char类型,但是里面都是数字
 
CREATE FUNCTION xh_LowTimeYSFW (@LowGlucose char(10),@curtime datetime,@Caseid int,@SchID int)RETURNS char(20)AS BEGIN DECLARE @d_retun char(20) SET @curtime = CAST(@curtime as datetime) select @d_retun = (DATEDIFF(hour,OldGlucoseTime,crtDate)*(@LowGlucose-GlucoseDoseFact)+ DATEDIFF(hour,crtDate,NewGlucoseTimeFact)*(CurGlucose-@LowGlucose))/(CurGlucose-GlucoseDoseFact) from (select top 1 OldGlucoseTime, CurGlucose,GlucoseDoseFact,NewGlucoseTimeFact,OldGlucoseTime,crtDate from SchemeDataTab where --CurGlucose>@LowGlucose and GlucoseDoseFact<@LowGlucose and Caseid=@Caseid and SchID=@SchID and DATEDIFF(hour,OldGlucoseTime,NewGlucoseTimeFact)<24) AA; return @d_retunEND-------子查询最后加个别名AA,另外,子查询中OldGlucoseTime字段不该出现2次。
 
(CurGlucose-GlucoseDoseFact)这个做被除数是否可能为0?
 
alter FUNCTION xh_LowTimeYSFW (@LowGlucose Char,@curtime datetime,@Caseid int,@SchID int)RETURNS char(20)AS BEGIN DECLARE @d_retun char(20) SET @curtime = CAST(@curtime as datetime)-- select @d_retun = ( DATEDIFF(hour,OldGlucoseTime,crtDate) * (Convert(Integer,@LowGlucose)-Convert(Integer,GlucoseDoseFact))-- + DATEDIFF(hour,crtDate,NewGlucoseTimeFact)*(CurGlucose-convert(integer,@LowGlucose)) )-- / (CurGlucose-GlucoseDoseFact) select @d_retun = DATEDIFF(hour,OldGlucoseTime,crtDate) * (Convert(Integer,@LowGlucose)-Convert(Integer,GlucoseDoseFact)) + DATEDIFF(hour,crtDate,NewGlucoseTimeFact)*(CurGlucose-convert(integer,@LowGlucose)) from (select top 1 OldGlucoseTime, CurGlucose,GlucoseDoseFact,NewGlucoseTimeFact, crtDate from SchemeDataTab where --CurGlucose>@LowGlucose and GlucoseDoseFact<@LowGlucose and Caseid=@Caseid and SchID=@SchID and DATEDIFF(hour,OldGlucoseTime,NewGlucoseTimeFact)<24) AA; return @d_retunEND---------------字符串至少在sql server中不可以想减,所以需要使用函数。上面的函数编译通过了,你再看一下@d_retun = 后面部分的表达式是否符合你的要求。
 
楼上的兄台比我还闲...哈哈
 
现在公司没什么工作,让我写文档,实在无聊啊。
 
znxia说得对,你先加个表别名再说吧.
 
报什么错呢?提示信息呢?
 
多人接受答案了。
 
后退
顶部