我有一个存储过程,我周三工作得很好,但不再正常工作。我没有改变任何代码,由于某种原因,它只是没有采取相同的行动。
以下是无效的代码段:
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语句,但是如果我不需要,我宁愿不要,特别是因为我之前有它工作。
由于
答案 0 :(得分: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
如果您需要更多帮助,如果您可以简化查询会很有帮助(这样做也可能有助于您找到问题)。