数据库自定义方法及在Sql语句中调用
由于 在我使用的数据表中 lasttime字段 在写入数据表时写入字符串‘20120401145112827’类型 在业务中需要对其字段时间做处理 以下过程是使用数据库自定义方法 简化Sql语句
select * from (select id,interID,cpid,intername,reply,lasttime,CASE lasttime WHEN '0' THEN getdate()-3 Else convert(datetime,dbo.StrToDateTime(lasttime),20) --该处调用function end as ldate,setTime from xsreuserinter ) as temp where (datediff(hh,temp.ldate,getdate())>72 and datediff(hh,temp.setTime,getdate())>72) order by lasttime desc
在此区别一下 function 和 procedure
procedure为独立存储过程,使用EXEC命令,而自定义函数可以在一个SQL语句内调用,也可使用exec命令调用。
数据库自定义方法可以简化sql语句 并且可以多次重复利用 :-) 以前都没用过 开心 顶一个!