来自sql查询的意外输出

时间:2014-09-11 12:25:02

标签: sql sql-server sql-server-2005

这是我的查询

DECLARE @AvgCharges TABLE (
    AverageTotalCharges MONEY
    ,ModeTotalCharges MONEY
    ,ServiceCode VARCHAR(20)
    ,SampleCount INT
    ,[EstimatedAllowedAmount] MONEY
    ,[Source] INT
    ,[Description] VARCHAR(200)
    )

INSERT INTO @AvgCharges
SELECT ISNULL(cpe.AverageTotalCharges, 0) AverageTotalCharges
    ,cpe.ModeTotalCharges
    ,svc.ServiceCode
    ,cpe.SampleCount
    ,EstimatedAllowedAmount = CASE 
        WHEN cpe.Mode = 1
            THEN AverageBP
        ELSE MaximumAllowedBP
        END
    ,1 [Source]
    ,''
FROM [Services] svc(NOLOCK)
INNER JOIN CPTPlanCodeEstimates cpe(NOLOCK) ON svc.ServiceCode = cpe.CPT4Code
WHERE svc.RegistrationID = 1918605
    AND cpe.StandardPatientType = 'O'
    AND cpe.PayorPlanCode = '101M'

SELECT *
FROM @avgcharges  -- here i have 1 record

PRINT @@ROWCOUNT  -- show 1

IF @@ROWCOUNT = 0
BEGIN
    INSERT INTO @AvgCharges (
        AverageTotalCharges
        ,ServiceCode
        ,[Source]
        )
    SELECT EstimatedCharges
        ,svc.ServiceCode
        ,2 [source]
    FROM [Services] svc(NOLOCK)
    INNER JOIN Estimates e(NOLOCK) ON svc.ServiceCode = e.Code
    WHERE svc.RegistrationID = 1918605
        AND PatientType = 'O'
        AND e.CodeType = (
            CASE 
                WHEN @PatientType = 'I'
                    THEN 'DRG'
                ELSE 'CPT'
                END
            )
    ORDER BY EstimatedCharges DESC
END

SELECT *
FROM @avgcharges   -- here I am getting 2 records in my table, which should be 1 actually.

我在我的表中得到两行,实际上应该是一行。我认为这里的if @@rowcount = 0条件并没有得到满足。

任何想法或替代方案?

请注意我使用的是SQL Server 2005 SP4

0 个答案:

没有答案