【问题标题】:SQL Server: A severe error occurred on the current command. The results, if any, should be discardedSQL Server:当前命令出现严重错误。结果,如果有的话,应该被丢弃
【发布时间】:2012-11-11 11:23:46
【问题描述】:

我在存储过程中有以下 SQL Server 查询,我正在从 Windows 应用程序运行此服务。我正在用 3000 万条记录填充临时表变量,然后将它们与 tbl_ref_test_main 中的前几天记录进行比较,以添加和删除不同的记录。在插入和删除时tbl_ref_test_main 上有一个触发器。触发器在另一个表中写入相同的记录。由于比较了 3000 万条记录,它需要很长时间才能产生结果并抛出错误说 A severe error occurred on the current command. The results, if any, should be discarded.

请有任何建议。

提前致谢。

-- Declare table variable to store the records from CRM database
DECLARE @recordsToUpload TABLE(ClassId NVARCHAR(100), Test_OrdID NVARCHAR(100),Test_RefId NVARCHAR(100),RefCode NVARCHAR(100));

-- Populate the temp table
INSERT INTO @recordsToUpload
SELECT 
class.classid AS ClassId,
class.Test_OrdID AS Test_OrdID ,    
CAST(ref.test_RefId AS VARCHAR(100)) AS Test_RefId, 
ref.ecr_RefCode AS RefCode                  
FROM  Dev_MSCRM.dbo.Class AS class 
LEFT JOIN Dev_MSCRM.dbo.test_ref_class refClass ON refClass.classid = class.classid
LEFT JOIN Dev_MSCRM.dbo.test_ref ref ON refClass.test_RefId = ref.test_RefId
WHERE class.StateCode = 0
AND (ref.ecr_RefCode IS NULL OR (ref.statecode = 0 AND LEN(ref.ecr_RefCode )<= 18 ))                      
AND LEN(class.Test_OrdID )= 12
AND ((ref.ecr_RefCode IS NULL AND ref.test_RefId IS NULL) 
OR (ref.ecr_RefCode IS NOT NULL AND ref.test_RefId IS NOT NULL));                       

-- Insert new records to Main table
INSERT INTO dbo.tbl_ref_test_main
Select * from @recordsToUpload 
EXCEPT 
SELECT * FROM dbo.tbl_ref_test_main;

-- Delete records from main table where similar records does not exist in temp table
DELETE P FROM dbo.tbl_ref_test_main AS P
WHERE EXISTS
(SELECT P.* 
EXCEPT
SELECT * FROM @recordsToUpload);

-- Select and return the records to upload
SELECT Test_OrdID,
CASE 
WHEN RefCode IS NULL THEN 'NA' 
ELSE RefCode 
END,
Operation AS 'Operation' 
FROM tbl_daily_upload_records 
ORDER BY Test_OrdID, Operation, RefCode;

【问题讨论】:

  • 问题很可能出现在触发器中,您是否尝试过调试?确保列类型、列大小和参数数量匹配
  • 此错误消息可能是 SQL Server 中的错误,因为它指示意外的内部状态或错误。还有其他消息吗?服务器日志显示什么?你能运行 DBCC CHECKDB 吗?它会打印任何错误吗?
  • 我在只有 2500 条记录的开发环境中测试过相同的应用程序和存储过程。但在预生产中,我有 3000 万条记录,它会在那里引发错误。我无权访问 pre prod,因此无法在那里运行 DBCC CHECKDB。我已经申请了许可,我也会尝试。
  • @MahaKhairy 谢谢。我已经测试了触发器并且它可以工作文件。
  • @Scorpion 你可以用临时表替换表变量并尝试逐语句执行proc语句以找到发生错误的地方

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2


【解决方案1】:

我的建议是 3000 万行对于 table 变量来说太大了,尝试创建一个临时表,用数据填充它,然后在那里执行分析。 如果这不可能/不合适,那么也许创建一个永久表并在使用之间截断它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多