all groups > sql server programming > november 2003 >
You're in the

sql server programming

group:

SP Question


Re: SP Question oj
11/19/2003 8:02:16 PM
sql server programming:
if there is no row you would get an empty resultset back. to force a
returned resultset with 1 row with a value of 0, you would have to do
something like this.

e.g.

CREATE PROCEDURE GetMachSalesTotalsNonStock
(
@SalesCookieID nvarchar(50)
)
AS
set nocount on
if exists(select sum(1) from tempSales INNER JOIN
tempSalesDetail ON tempSales.tempSalesID =
tempSalesDetail.tempSalesID
WHERE (tempSales.tempSalesQuidID = @SalesCookieID)
GROUP BY tempSalesDetail.NonStock
HAVING (tempSalesDetail.NonStock = 1)
)
SELECT
theSubTotalNonStock = SUM(CASE
WHEN tempSalesDetail.SalesPrice Is Null THEN 0
ELSE tempSalesDetail.SalesPrice
END
)
FROM tempSales INNER JOIN
tempSalesDetail ON tempSales.tempSalesID =
tempSalesDetail.tempSalesID
WHERE (tempSales.tempSalesQuidID = @SalesCookieID)
GROUP BY tempSalesDetail.NonStock
HAVING (tempSalesDetail.NonStock = 1)
else
select 0
GO

--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

SP Question dave
11/19/2003 10:26:24 PM
What am I missing here. When there is no records I want to return a 0. It
does not do that???

CREATE PROCEDURE GetMachSalesTotalsNonStock
(
@SalesCookieID nvarchar(50)
)
AS
SELECT
theSubTotalNonStock = SUM(CASE
WHEN tempSalesDetail.SalesPrice Is Null THEN 0
ELSE tempSalesDetail.SalesPrice
END
)
FROM tempSales INNER JOIN
tempSalesDetail ON tempSales.tempSalesID =
tempSalesDetail.tempSalesID
WHERE (tempSales.tempSalesQuidID = @SalesCookieID)
GROUP BY tempSalesDetail.NonStock
HAVING (tempSalesDetail.NonStock = 1)
GO

Re: SP Question Louis Davidson
11/19/2003 10:46:45 PM
What is it returning? I am assuming null, since you are summing all null
values. For example:


--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

Re: SP Question Louis Davidson
11/19/2003 10:48:32 PM
What is it returning? I am assuming null, since you are summing all null
values. For example:

select sum(case when unitPrice is null then 0 else unitPrice end)
from products
where 1=2

returns null. Add coalesce to 0:

select coalesce (sum(case when unitPrice is null then 0 else unitPrice
end),0)
from products
where 1=2

This returns the proper value.

----------------------------------------------------------------------------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]


Re: SP Question dave
11/19/2003 11:01:38 PM
It gives me no column name??

select coalesce (sum(case when tempSalesDetail.SalesPrice is null then 0
else tempSalesDetail.SalesPrice
end),0)
FROM tempSales INNER JOIN
tempSalesDetail ON tempSales.tempSalesID =
tempSalesDetail.tempSalesID
WHERE (tempSales.tempSalesQuidID =
'00ba8a46-a794-443a-9044-d25fa55c0cbf')
GROUP BY tempSalesDetail.NonStock
HAVING (tempSalesDetail.NonStock = 1)



[quoted text, click to view]

Re: SP Question Louis Davidson
11/19/2003 11:15:16 PM
Sorry:

select coalesce (sum(case when tempSalesDetail.SalesPrice is null then 0
else tempSalesDetail.SalesPrice
end),0) as theSubTotalNonStock --<<--Added the as
FROM tempSales INNER JOIN
tempSalesDetail ON tempSales.tempSalesID =
tempSalesDetail.tempSalesID
WHERE (tempSales.tempSalesQuidID =
'00ba8a46-a794-443a-9044-d25fa55c0cbf')
GROUP BY tempSalesDetail.NonStock
HAVING (tempSalesDetail.NonStock = 1)

--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

AddThis Social Bookmark Button