首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL函数返回的结果与普通查询不同

问SQL函数返回的结果与普通查询不同
EN

Stack Overflow用户
提问于 2012-12-13 23:20:11
回答 1查看 71关注 0票数 0

我使用的是SQL Server 2008R2,我有以下脚本。

代码语言:javascript
复制
select * from orderSummaryTotal(@orderid,@sessionid)

select
    count(*) as Quantity,
    IsNull(Sum(VatAmount),0) As VATAmount,
    IsNull(Sum(NetAmount),0) As NetAmount,
    IsNull(Sum(GrossAmount),0) as GrossAmount
from tbOrderProduct
where
     Orderid = @orderid
 and sessionid = @sessionid

当我运行第二个查询时,它将返回值。即数量为3

但是,当我运行第一个查询时,它返回的数量为0。

第一个查询是一个表值函数,下面是代码。

代码语言:javascript
复制
ALTER FUNCTION [dbo].[OrderSummaryTotal](@orderid varchar, @sessionid uniqueidentifier)
RETURNS TABLE as
RETURN
  select
      count(*) as Quantity,
      IsNull(Sum(VatAmount),0) As VATAmount,
      IsNull(Sum(NetAmount),0) As NetAmount,
      IsNull(Sum(GrossAmount),0) as GrossAmount
  from tbOrderProduct
  where
       Orderid = @orderid
   and sessionid = @sessionid

这两个查询是相同的,但是为什么一个返回计数为3,而另一个不是呢?有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-13 23:25:58

原因是在您的函数定义中有没有长度的varchar。

尝试将其更改为类似于varchar(8000)的值,或者更改为一个足够大以满足您的需要的数字。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13862919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档