当前位置: 代码迷 >> SQL >> 求1sql存储过程语句
  详细解决方案

求1sql存储过程语句

热度:16   发布时间:2016-05-05 12:38:21.0
求一sql存储过程语句。
是这样的,
这个存储过程有两个变量,分别为 @time1,@time2,都是datetime型,主要查询这两个字段之间的数据,用between就可以。
但是如果其中有一个为空,[email protected][email protected][email protected][email protected][email protected]?个,但是运行不行,提示运行命令成功,并不出来我想要的表。
我写的大概如下:
create proc searchdata
@time1 datetime,
@time2 datetime
as
if(@time1<>null and @time2<>null)
begin
select * from table where addtime bewteen @time1 and @time2
end
else if(@time1<>null or @time2<>null)
begin
if @time1<>null
begin
.....
end
end
..........
就这个意思,大侠一看就懂,看看哪里错了。
exec searchdata ‘2009-01-01’,‘2009-12-31’
结果为:命令已成功完成。
请帮。为感@!!!!
------解决方案--------------------
if(@time1 IS NOT null and @time2 IS NOT null)

else if(@time1 IS NOT null or @time2 IS NOT null)
------解决方案--------------------
if(@time1 <>null and @time2 <>null)

与NULL的任何相关运算的结果还是NULL, 所以要用 IS NULL, 或者 ISNULL() 来判断是否为NULL
@time1 is Null 
isnull(@time1)
  相关解决方案