存储过程在7年后随机停止工作

时间:2013-10-28 17:31:40

标签: sql sql-server vb.net stored-procedures

我为一家对冲基金工作,我们的数据库系统最近开始采取行动。我做了一些调查,实现了错误代码以及每个进程之间的消息框。我终于找到了问题的确切位置:它似乎在SQL中的存储过程中。

我们在Windows XP SP3上使用VB 2005,Access for SQL表视图和Microsoft Server Management Studio Express。

其他所有流程都在运作。以下是它的工作原理:

我们从bloomberg填写一个创建.csv文件的事务。此.csv文件放入名为BBGT_Transactions的SQL表中。这是一份直接的副本。这个过程非常完美。每次运行过程(每30分钟),交易就在那里。接下来,相同的过程接受BBGT_Transactions中的事务并将其复制到Transactions。来自VB的调用如下:

 Public Sub CopyNewEMSTransactions()

        Log.Info("Copying new transactions from BBGTransactions to Transactions")
        DAL.sqlcmd("CopyNewEMSTransactions")

End Sub

CopyNewEMSTransactions是一个存储过程,如下所示:

USE [IPAS]
GO
/****** Object:  StoredProcedure [dbo].[CopyNewEMSTransactions]    Script Date: 10/28/2013 13:34:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Name
-- Create date: 
-- Description: 
-- =============================================
ALTER PROCEDURE [dbo].[CopyNewEMSTransactions] 
    -- Add the parameters for the stored procedure here
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

--New Code: Accounts for option trades through bloomberg terminal
INSERT INTO Transactions (Account, TDate, [Time], SDate, Class,
                              [Type], Ticker, Quantity, Price, SEDOL,
                              CUSIP, Comments, OrderNumber, ISIN)
    SELECT TranAccount AS Account,
           (SELECT CAST(FLOOR(CAST(ExecDate AS float)) AS datetime)) AS TDate,
           ExecDate + ExecTime - '1899-12-30' AS [Time],
           ExecDate + 3 AS SDate, CASE EMS.Broker
                                    WHEN 'CIBC' THEN 'Equity'
                                    WHEN 'CIBO' THEN 'Option' END AS Class,
          CASE Side
            WHEN 'SS' THEN 'SHORT'
            WHEN 'B' THEN 'BUY'
            WHEN 'S' THEN 'SELL'
            WHEN 'BS' THEN 'BUY'
            ELSE 'UNKNOWN' 
          END AS Type, EMS.Ticker,
          CASE Side
            WHEN 'SS' THEN -1
            WHEN 'S' THEN -1
            WHEN 'B' THEN 1
            WHEN 'BS' THEN 1
          END * FillAmount AS Quantity, 
          AveragePrice AS Price,
          SEDOL, CUSIP,
          Comments = 'Bloomberg data',
          LatestRows.OrderNumber, ISIN
    FROM    
        (SELECT Ticker, OrderNumber, MAX(ExecSeqNumber) AS LastExecSeqNumber
        FROM BBGTransactions
        WHERE OrderNumber NOT IN (SELECT OrderNumber FROM Transactions 
                                  WHERE Comments = 'Bloomberg data')

        GROUP BY OrderNumber, Ticker) LatestRows
    LEFT JOIN
        --Changed from "SELECT * From BBGTransactions" to add the ' Equity'
        (SELECT BBG_ID, ExecDate, ExecTime, TranAccount, 
            CASE Broker
                WHEN 'CIBC' THEN Ticker
                WHEN 'CIBO' THEN Ticker + ' Equity' END AS Ticker,
        --With option trades, SEDOL is not used anywhere. It used to be used for the Reconciliation report
        --but that report has been changed to look at option ticker instead.
        CASE Broker
            WHEN 'CIBC' THEN SEDOL
            WHEN 'CIBO' THEN NULL END AS SEDOL, 
        CUSIP, OrderNumber, Side, FillAmount, AveragePrice, ExecLastFillPX, Broker, Currency, OrderType, 
        LimitPrice, DayFillAmount, DayAvgPrice, ISIN, Amount, ExecType, ExecSeqNumber, ExecPrevSeqNumber FROM BBGTransactions) EMS
    ON LatestRows.LastExecSeqNUmber = EMS.ExecSeqNumber
       AND LatestRows.OrderNumber = EMS.OrderNumber
    WHERE LatestRows.OrderNumber NOT IN (SELECT OrderNumber FROM Transactions 
                              WHERE Comments = 'Bloomberg data')

END

它不返回任何错误并显示“将BBGT复制到事务表”,然后“完成”但不复制!!它工作了7年,并在大约3周前停止工作。

要记住的事情:

  • 上个月我们有非常断断续续的互联网
  • 我怀疑Windows更新已运行且Windows在此之前约3年未更新。我上次检查时有70次更新,现在没有。
  • 回到幽灵是我的最后一招,因为我需要做很多工作来实施
  • 没有代码改变了!
  • 我已经尝试过只运行存储过程而且没有做任何事情,如怀疑。

修改 有一个跟踪器日志,只运行该过程。它没有返回任何错误?

SELECT SYSTEM_USER
go
SET ROWCOUNT 0 SET TEXTSIZE 2147483647 SET NOCOUNT OFF SET CONCAT_NULL_YIELDS_NULL ON SET ARITHABORT ON SET LOCK_TIMEOUT -1 SET QUERY_GOVERNOR_COST_LIMIT 0 SET DEADLOCK_PRIORITY NORMAL SET TRANSACTION ISOLATION LEVEL READ COMMITTED SET ANSI_NULLS ON SET ANSI_NULL_DFLT_ON ON SET ANSI_PADDING ON SET ANSI_WARNINGS ON SET CURSOR_CLOSE_ON_COMMIT OFF SET IMPLICIT_TRANSACTIONS OFF SET QUOTED_IDENTIFIER ON
go
select @@spid select SERVERPROPERTY('ProductLevel')
go
USE [IPAS]

go

DECLARE @return_value int

EXEC    @return_value = [dbo].[CopyNewEMSTransactions]

SELECT  'Return Value' = @return_value


go
exec sp_execute 18,5930,5924,5925,5926,5927,5928,5929,5923,5921,5922
go
exec sp_execute 16,59826,59827,59756,59757,59758,59716,59715,59700,59701,59702
go

另外,相应的excel表:

http://i.imgur.com/Ce76EHd.png

链接,因为很难看到:

http://i.stack.imgur.com/iJFRo.png


选择声明#1(似乎正在工作):

SELECT Ticker, OrderNumber, MAX(ExecSeqNumber) AS LastExecSeqNumber
FROM BBGTransactions
WHERE OrderNumber NOT IN (SELECT OrderNumber FROM Transactions
                          WHERE Comments = 'Bloomberg data')
GROUP BY OrderNumber, Ticker

返回: http://imgur.com/3A8E1JR

链接:http://imgur.com/3A8E1JR

选择2:

CODE:

SELECT BBG_ID, ExecDate, ExecTime, TranAccount,
    CASE Broker
        WHEN 'CIBC' THEN Ticker
        WHEN 'CIBO' THEN Ticker + ' Equity' END AS Ticker,
    --With option trades, SEDOL is not used anywhere. It used to be used for the Reconciliation report
    --but that report has been changed to look at option ticker instead.
    CASE Broker
        WHEN 'CIBC' THEN SEDOL
        WHEN 'CIBO' THEN NULL END AS SEDOL,
    CUSIP, OrderNumber, Side, FillAmount, AveragePrice, ExecLastFillPX, Broker, Currency, OrderType,
    LimitPrice, DayFillAmount, DayAvgPrice, ISIN, Amount, ExecType, ExecSeqNumber, ExecPrevSeqNumber 
    FROM BBGTransactions
    WHERE LatestRows.OrderNumber NOT IN (SELECT OrderNumber FROM Transactions
                                         WHERE Comments = 'Bloomberg data')

ERROR 服务器:消息4104,级别16,状态1,行1 无法绑定多部分标识符“LatestRows.OrderNumber”。

3 个答案:

答案 0 :(得分:2)

您为对冲基金工作,获得标准许可,不使用免费赠品。

它看起来不像您发布了实际的存储过程,而是“清理”版本以显示它是如何工作的。这实际上是一种阻碍,而不是一种帮助。

很难说这个删除,但我会说你有一个数据问题(查询运行完全像它一直有,但你有的数据不会返回任何结果),或者你最终超过快递版本中的限制。

忘记运行SP,提取查询并直接运行它们,了解数据的实际外观。如果选择有效而不是插入,则可能超出了快速版本的限制。如果select没有返回预期的行,那么就会出现数据问题。

答案 1 :(得分:1)

-I have tried running ONLY the stored procedure and it does nothing.

您期望它返回什么?它会显示任何受影响的行吗?

NO CODE WAS CHANGED EVER

您确定架构没有更改吗?您是否尝试运行查询并亲自查看它是否有效? (在没有来自SP的insert命令的情况下运行select)

您确定使用的是正确的数据库吗?这也可能会改变..

你确定你的VB代码真的完成了吗?我会在执行SP后尝试打印日志,这里可能有一个我们看不到的例外。

答案 2 :(得分:0)

计算机不做"随机" .. 有些事情发生了变化,导致了意想不到的结果。单步执行代码是确定的唯一方法..