首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一sql存储过程语句。该怎么处理

2012-02-29 
求一sql存储过程语句。是这样的,这个存储过程有两个变量,分别为 @time1,@time2,都是datetime型,主要查询这

求一sql存储过程语句。
是这样的,
这个存储过程有两个变量,分别为 @time1,@time2,都是datetime型,主要查询这两个字段之间的数据,用between就可以。
但是如果其中有一个为空,比如@time1为空,这就查询@time1以后的数据,如果@time2为空,则查询@time2以前的数据,如果都为空则查询所有数据。如后搞。自己写了一个,但是运行不行,提示运行命令成功,并不出来我想要的表。
我写的大概如下:
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)

热点排行