假設我有一個表,它包含個人和公司的一些數據,並且如果沒有爲個人或公司找到數據,我想檢索默認值,例如,假設我有一個表sql如果未找到結果,請選擇默認行
CustomerAccountId CustomerAccountType DueDays IsAdjustable isDefaultForIndividual isDefaultForCompany
1 Individual 10 true false false
2 Company 20 false false false
null null 5 false true false
null null 30 true false true
我想創建一個函數有兩個參數IndividualCustomerAccountId
和CompanyCustomerAccountId
如果IndividualCustomerAccountId
在表中找到檢索它,如果沒有找到檢索個人的默認值,它是在這種情況下, the same for companies, if the
CompanyCustomerAccountId的third row
is found retrieve it, if not get the default value for the companies which is
第四行在這種情況下。
假設我們創建了接受IndividualCustomerAccountId
作爲第一個參數和CompanyCustomerAccountId
作爲第二個參數funtion
樣品輸入
MyFunc(1 , 2)
應該返回first and second rows
MyFunc(1 , 50)
應該返回first and fourth rows
,因爲沒有一家公司與CustomerAccountId
50
在表中找到所以檢索c的默認值ompanies因爲客戶的賬戶ID 100
沒有任何人士被發現這是fourth row
MyFunc(100 , 2)
應該返回second and third rows
所以我們得到了個人的默認值是third row
,我們有customerAccountId
2一個公司,所以我們根本就retrive它從桌子上。
我想創建無論是LINQ查詢或SQL函數來實現這些結果
[這可能有助於](http://stackoverflow.com/a/285823/5707687) –
我將假設正常化您的數據不是一種選擇?更好的設計會更容易查詢。 – Crowcoder
@Crowcoder你是對的,但不幸的是,我們現在不能改變DB設計 –