2017-04-09 46 views
1

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

我想创建一个函数有两个参数IndividualCustomerAccountIdCompanyCustomerAccountId如果IndividualCustomerAccountId在表中找到检索它,如果没有找到检索个人的默认值,它是在这种情况下, the same for companies, if the CompanyCustomerAccountId的third rowis 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,因为没有一家公司与CustomerAccountId50在表中找到所以检索c的默认值ompanies因为客户的账户ID 100没有任何人士被发现这是fourth row

MyFunc(100 , 2)应该返回second and third rows所以我们得到了个人的默认值是third row,我们有customerAccountId 2一个公司,所以我们根本就retrive它从桌子上。

我想创建无论是LINQ查询或SQL函数来实现这些结果

+0

[这可能有助于](http://stackoverflow.com/a/285823/5707687) –

+0

我将假设正常化您的数据不是一种选择?更好的设计会更容易查询。 – Crowcoder

+0

@Crowcoder你是对的,但不幸的是,我们现在不能改变DB设计 –

回答

2

你可以尝试SQL函数

CREATE TABLE Customer 
(
    CustomerAccountId int, 
    CustomerAccountType varchar(20), 
    DueDays int, 
    IsAdjustable bit, 
    isDefaultForIndividual bit, 
    isDefaultForCompany bit 
) 

INSERT INTO Customer VALUES 
(1, 'Individual', 10, 1,0,0), 
(2, 'Company', 20, 0,0,0), 
(null, null, 5, 0,1,0), 
(null, null, 30, 1,0,1) 

GO 

CREATE FUNCTION MyFunc 
(
@IndividualCustomerAccountId int, 
@CompanyCustomerAccountId int 
) 
RETURNs @result TABLE 
(
    CustomerAccountId int, 
    CustomerAccountType varchar(20), 
    DueDays int, 
    IsAdjustable bit, 
    isDefaultForIndividual bit, 
    isDefaultForCompany bit 
) 
BEGIN 

    INSERT INTO @result 
    SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
    FROM Customer c 
    WHERE (CustomerAccountId = @IndividualCustomerAccountId AND CustomerAccountType = 'Individual') 
      OR (CustomerAccountId = @CompanyCustomerAccountId AND CustomerAccountType = 'Company') 

    IF(NOT EXISTS (SELECT 1 FROM Customer c 
        WHERE CustomerAccountId = @IndividualCustomerAccountId AND CustomerAccountType = 'Individual')) 
    BEGIN 
     INSERT INTO @result 
     SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
     FROM Customer c 
     WHERE CustomerAccountId IS NULL AND isDefaultForIndividual = 1 
    END 

    IF(NOT EXISTS (SELECT 1 FROM Customer c 
        WHERE CustomerAccountId = @CompanyCustomerAccountId AND CustomerAccountType = 'Company')) 
    BEGIN 
     INSERT INTO @result 
     SELECT CustomerAccountId , CustomerAccountType, DueDays, IsAdjustable, isDefaultForIndividual,isDefaultForCompany 
     FROM Customer c 
     WHERE CustomerAccountId IS NULL AND isDefaultForCompany = 1 
    END 

    RETURN; 
END 

GO 


SELECT * from dbo.MyFunc(1,2) 
SELECT * from dbo.MyFunc(1,50) 
SELECT * from dbo.MyFunc(100,2) 
SELECT * from dbo.MyFunc(100,50) 
--DROP TABLE Customer 

演示链接:Rextester

0

基本上,你可以运行若您在使用查询存在,看看是否有将是任何数据。如果是这样,请继续并运行您的查询。如果不是,请执行选择默认行。

If Exists (your query) 

     (your query) 

Else 

     SELECT 'query for default row' 

我希望它清楚你的问题。
快乐Coding.Thanks