[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

How to return 0 if now rows in query?

Asked by melegant99 in Databases Miscellaneous, SQL Server 2005

This following block of SQL does a calculation:
UPDATE labordata SET rmrtotal = t1.sumtotal FROM labordata INNER JOIN
(SELECT IsNull(SUM((((labordata.cost * labordata.qty) * (mbillcode.rmrperc / 100.0)) * (1 - (mbillcode.discper / 100)))),0) as sumtotal,
labordata.sanum,labordata.jobnum,labordata.altnum,labordata.docid,labordata.laborid,mbillcode.onfield,mbillcode.fixed
FROM mbillcode INNER JOIN labordata ON
labordata.sanum = mbillcode.sanum AND labordata.jobnum = mbillcode.jobnum AND labordata.altnum = mbillcode.altnum AND labordata.docid = mbillcode.docid
AND labordata.fldr = 'none' and labordata.laborid = mbillcode.laborid
WHERE mbillcode.sanum = @sanum and mbillcode.jobnum = @jobnum and mbillcode.altnum = @altnum and mbillcode.docid = @docid AND mbillcode.onfield = 'Labor Hours Cost' AND mbillcode.fixed = 0
GROUP BY labordata.sanum,labordata.jobnum,labordata.altnum,labordata.docid,labordata.laborid,mbillcode.onfield,mbillcode.fixed)
t1 ON t1.sanum = labordata.sanum AND t1.jobnum = labordata.jobnum AND t1.altnum = labordata.altnum  AND t1.docid = labordata.docid
WHERE labordata.sanum = @sanum AND labordata.jobnum = @jobnum AND labordata.altnum = @altnum AND labordata.docid = @docid AND labordata.laborid = t1.laborid AND t1.onfield = 'Labor Hours Cost' AND t1.fixed = 0 and labordata.fldr = 'none'

If there are no rows returned from the joins, I need sumtotal to = 0. I was thinking to do an IF @@ROWCOUNT after but maybe there is a better way?

IF @@ROWCOUNT = 0 SET etc etc....

Thanks.
[+][-]10/28/09 07:48 AM, ID: 25683840Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/28/09 07:49 AM, ID: 25683849Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/30/09 04:44 AM, ID: 25701926Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_3_20080625