如何解析从T-SQL查询返回的行

时间:2016-05-06 22:20:39

标签: sql sql-server

假设我在SQL Server中有一个表(让我们称之为Audit)

AuditID int primary not null,
Name varchar(300),
AuditCount int,
ActualCount int,
AuditDate datetime

该表可以包含任意数量的行,其中包含给定的AuditID。我想在给定AuditID的情况下遍历表,并将AuditCount与ActualCount进行比较。如果它们不相等,那么我会做其他事情......但首先,我如何一次检索一行,并比较返回的值?

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
    AuditID,
    Name,
    AuditCount,
    ActualCount,
    AuditDate
FROM Audit
WHERE AuditCount <> ActualCount; 

SELECT
        AuditID,
        Name,
        case when AuditCount <> ActualCount then 'Do something'
        else 'do something else' end as myColumn,
        AuditDate
FROM Audit;