1

我有一个存储过程,我在星期三工作得很好,但不再正常工作。我没有更改任何代码,并且由于某种原因它的行为方式不同。

这是不起作用的代码段:

WITH Minimum AS (SELECT DataTable.PortID, [Rating Max],
    CASE When [Rating Max] <= [Sector Max] And [Rating Max] <= [ConcAfterUnwritten] And [Rating Max] <= [NatAfterUnwritten] And [Rating Max] <= [CashMaxAfterUnwritten] And [Rating Max] <= MatAfterUnwritten And [Rating Max] <= MatAfterInitialConcentration  Then [Rating Max]
         When [Sector Max] <= [ConcAfterUnwritten] And [Sector Max] <= [NatAfterUnwritten] And [Sector Max] <= CashMaxAfterUnwritten And [Sector Max] <= MatAfterUnwritten And [Sector Max] <= MatAfterInitialConcentration Then [Sector Max]
         When ConcAfterUnwritten <= NatAfterUnwritten And ConcAfterUnwritten <= CashMaxAfterUnwritten And ConcAfterUnwritten <= MatAfterUnwritten And ConcAfterUnwritten <= MatAfterInitialConcentration Then ConcAfterUnwritten
         When NatAfterUnwritten <= CashMaxAfterUnwritten And NatAfterUnwritten <= MatAfterUnwritten And NatAfterUnwritten <= MatAfterInitialConcentration Then NatAfterUnwritten
         When CashMaxAfterUnwritten <= MatAfterUnwritten And CashMaxAfterUnwritten <= MatAfterInitialConcentration Then CashMaxAfterUnwritten
         When MatAfterUnwritten <= MatAfterInitialConcentration Then MatAfterUnwritten
         Else MatAfterInitialConcentration
         End As [Min Of 5 Restrictions]
FROM DataTable)
UPDATE DataTable
Set 
    DataTable.MinOf5Restrictions = Minimum.[Min Of 5 Restrictions],
    DataTable.MktValueAllocation = IIF(Minimum.[Min Of 5 Restrictions] < DataTable.MatAfterInitialConcentration, 0, DataTable.MatAfterInitialConcentration),
    DataTable.ModelParAmount = IIf([MktValueAllocation]=0,0,Round([MktValueAllocation]/([$Px+Acc]*10)/5,0,1)*5),
    DataTable.AllocatedPar = IIF(DataTable.ModelParAmount = 0, 0, ParLots.AllocatedPar),
    DataTable.[Check Cash And Concentration] = IIf(DataTable.[AllocatedPar]*[$Px+Acc]*10>[MinOf5Restrictions],[ModelParAmount],DataTable.[AllocatedPar]),
    DataTable.[Final After State Switch] =  IIf([StateOnly]='TRUE',IIf([RestrictionType]='Specific',[Check Cash And Concentration],0),[Check Cash And Concentration])
FROM 
    ((DataTable INNER JOIN Minimum ON DataTable.PortID = Minimum.PortID) LEFT JOIN ParLots ON DataTable.ModelParAmount = ParLots.ModelPar) INNER JOIN TestSet ON DataTable.PortID = TestSet.PortID, Main;

我目前的问题是我必须运行查询 6 次才能获得我想要的结果。基本上,UPDATE 语句似乎只在我每次执行它时更新其中一个列,而不是一次更新所有 6 个。第一次执行时 MinOf5Restrictions 已填充但其他列未填充,第二次填充 MktValueAllocation 但最后 4 列仍为空,等等。

知道为什么会这样吗?我想我可以将它分成 6 个不同的 UPDATE 语句,但如果我不必这样做,我宁愿不这样做,特别是因为我之前有它工作过。

谢谢

4

1 回答 1

2

您在那里的查询非常复杂,如果不知道表的架构和表中的值,就很难诊断出您的问题。但是,据说我通常调试更新问题的方式是将其修改为选择并查看返回的值是否是您期望的以及您想要更新的值。所以我会将查询更新为这样的内容(无法验证这是否有效,因为我没有架构,但如果没有架构,应该给你一个想法)。

WITH Minimum AS 
(
    SELECT 
        DataTable.PortID
       ,[Rating Max]
       ,CASE 
            When [Rating Max] <= [Sector Max] And [Rating Max] <= [ConcAfterUnwritten] And [Rating Max] <= [NatAfterUnwritten] And [Rating Max] <= [CashMaxAfterUnwritten] And [Rating Max] <= MatAfterUnwritten And [Rating Max] <= MatAfterInitialConcentration  Then [Rating Max]
            When [Sector Max] <= [ConcAfterUnwritten] And [Sector Max] <= [NatAfterUnwritten] And [Sector Max] <= CashMaxAfterUnwritten And [Sector Max] <= MatAfterUnwritten And [Sector Max] <= MatAfterInitialConcentration Then [Sector Max]
            When ConcAfterUnwritten <= NatAfterUnwritten And ConcAfterUnwritten <= CashMaxAfterUnwritten And ConcAfterUnwritten <= MatAfterUnwritten And ConcAfterUnwritten <= MatAfterInitialConcentration Then ConcAfterUnwritten
            When NatAfterUnwritten <= CashMaxAfterUnwritten And NatAfterUnwritten <= MatAfterUnwritten And NatAfterUnwritten <= MatAfterInitialConcentration Then NatAfterUnwritten
            When CashMaxAfterUnwritten <= MatAfterUnwritten And CashMaxAfterUnwritten <= MatAfterInitialConcentration Then CashMaxAfterUnwritten
            When MatAfterUnwritten <= MatAfterInitialConcentration Then MatAfterUnwritten
            Else MatAfterInitialConcentration
         End As [Min Of 5 Restrictions]
    FROM DataTable
)
SELECT
    Minimum.[Min Of 5 Restrictions] [MinOf5Restrictions],
    IIF(Minimum.[Min Of 5 Restrictions] < DataTable.MatAfterInitialConcentration, 0, DataTable.MatAfterInitialConcentration) [MktValueAllocation],
    IIf([MktValueAllocation]=0,0,Round([MktValueAllocation]/([$Px+Acc]*10)/5,0,1)*5) [ModelParAmount],
    IIF(DataTable.ModelParAmount = 0, 0, ParLots.AllocatedPar) [AllocatedPar],
    IIf(DataTable.[AllocatedPar]*[$Px+Acc]*10>[MinOf5Restrictions],[ModelParAmount],DataTable.[AllocatedPar]) [Check Cash And Concentration],
    IIf([StateOnly]='TRUE',IIf([RestrictionType]='Specific',[Check Cash And Concentration],0),[Check Cash And Concentration]) [Final After State Switch]
FROM 
    ((DataTable 
    INNER JOIN Minimum ON 
        DataTable.PortID = Minimum.PortID) 
    LEFT JOIN ParLots ON 
        DataTable.ModelParAmount = ParLots.ModelPar) 
    INNER JOIN TestSet ON DataTable.PortID = TestSet.PortID

如果您需要更多帮助,如果您可以简化查询(这样做也可以帮助您找到问题),将会很有帮助。

于 2013-07-08T17:15:58.247 回答