I am using SQL Server 2008R2 and I have the following scripts.
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
When I run the Second Query it returns me values. Namely a Quantity of 3
However when I run the first Query it returns me a Quantity of 0.
The First Query is a Table Valued Function Here is the code.
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
Both queries are identical but how come one returns a count of 3 and the other does not? Any ideas?
The reason is that you have
varcharin your function definition with no length.Try changing it to something like
varchar(8000), or a number large enough to suit your needs.