如果找不到结果,则sql选择默认行

时间:2017-04-09 12:57:18

标签: c# sql sql-server linq

假设我有一个表,其中包含个人和公司的一些数据,如果没有找到个人或公司的数据,我想检索默认值,例如,假设我有一个表

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检索它,则需要两个参数CompanyCustomerAccountIdIndividualCustomerAccountId,如果找不到则检索个人的默认值third row {1}}在这种情况下, the same for companies, if the CompanyCustomerAccountId is found retrieve it, if not get the default value for the companies which is在这种情况下为第四行。

假设我们创建了一个功能,接受IndividualCustomerAccountId作为第一个参数,CompanyCustomerAccountId作为第二个参数

示例输入

MyFunc(1 , 2)应该返回first and second rows

MyFunc(1 , 50)应该返回first and fourth rows,因为在表格中找不到CustomerAccountId 50的公司,因此检索fourth row公司的默认值

MyFunc(100 , 2)应该返回second and third rows,因为找不到具有客户帐户ID 100的个人,因此我们获得了third row个人的默认值,我们有一个公司customerAccountId 2,所以我们只需从表中检索它。

我想创建一个LINQ查询或SQL函数来实现这些结果

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

If Exists (your query)

       (your query)

Else

        SELECT 'query for default row'

我希望它清楚你的问题 Happy Coding.Thanks