日期:2014-05-18  浏览次数:20544 次

一个关于datetime类型的模糊查询
编写一存储过程,查询指定客户在指定时间段内购买指定产品的数量,存储过程中使用了输入和输出参数。并调用该存储过程查询名称为“家电市场”的客户在2004年购买“洗衣机”的数量。

SQL code

create function getStock2(@customerName char(30),@time datetime,@productName char(30))
                returns int
as
begin
     declare @Stock int;
     select @Stock=cpxsb.数量
     from cp,cpxsb,xss
     where cp.产品编号=cpxsb.产品编号 and cpxsb.客户编号=xss.客户编号 and 客户名称=@customerName and  产品名称=@productName and 销售日期=@time
     return @Stock
end



然后测试的时候,@time参数那边就会有问题:要具体到月日,而不能做到题目要求的那样在2004年购买的

望哪位可以帮我这新手解答或提示,在此谢过。

------解决方案--------------------
SQL code
create function getStock2(@customerName char(30),@time datetime,@productName char(30))
                returns int
as
begin
     declare @Stock int;
     select @Stock=cpxsb.数量
     from cp,cpxsb,xss
     where cp.产品编号=cpxsb.产品编号 and cpxsb.客户编号=xss.客户编号 and 客户名称=@customerName and  产品名称=@productName and 销售日期=year(@time)
     return @Stock
end

------解决方案--------------------
探讨

引用:
如果没看错的话 这个叫做函数 而不是存储过程。

谢谢啊,新手
然后我测试的时候 select dbo.getStock2('家电市场',2004,'洗衣机') as 数量
还是不行。。。select dbo.getStock2('家电市场','2004','洗衣机') as 数量 也不行,怎么解决。