假设我有一个表,它包含个人和公司的一些数据,并且如果没有为个人或公司找到数据,我想检索默认值,例如,假设我有一个表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设计 –