Return a value if no record is found

后端 未结 4 2104
悲哀的现实
悲哀的现实 2020-11-28 08:36

I have this simple statement that works:

SELECT idnumber FROM dbo.database WHERE number = \'9823474\'

If the number does not exist anywhere

4条回答
  •  Happy的楠姐
    2020-11-28 09:15

    Select isnull(sum(Amount),0) as Amt from BeginningBalance where CustomerID = @CustomerID
    Union all
    Select isnull(sum(Amount),0) as Amt from SalesOrders where CustomerID = @CustomerID
    Union all
    Select isnull(sum(Amount),0) as Amt from SalesInvoices where CustomerID = @CustomerID
    //Data Row Result if no data is present at Beginning Balance Table
    // example 
    
    Amt
    2000  // amount from sales orders
    1000  // amount from sales invoices
    

    // if the 1st select statement return no data use this
    SELECT (select sum(Amount) from BeginningBalance 
            where CustomerID = @CustomerID) as Amt
    Union all
    Select sum(Amount) as Amt from SalesOrders where CustomerID = @CustomerID
    Union all
    Select sum(Amount) as Amt from SalesInvoices where CustomerID = @CustomerID
    

    Result :

    Amt
    NULL  // amount from BeginningBalance
    2000  // amount from sales orders
    1000  // amount from sales invoices
    

提交回复
热议问题