【问题标题】:A severe error occurred on the current command. - Unknown Error当前命令发生严重错误。 - 未知错误
【发布时间】:2014-05-12 06:08:15
【问题描述】:

这是我的代码。

;With CTE as
(
select
    a.rn,
    a.LongDescription as ad,
    b.LongDescription as bd
from myTabl as a
    left join myTabl as b on a.rn +1 = b.rn
where
    a.rn=1
and
    a.LongDescription = 'Eric'
)
update CTE
    set ad += ' ' + b.LongDescription
from CTE as a
    left join myTabl as b on a.rn = b.rn+1
where
    a.rn=1

我在尝试 diff 时尝试回答一个问题。我遇到以下错误的选项,

Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.

这是什么,我什么都没有。我的代码有什么重大错误吗?我试图用谷歌搜索它,但所有结果都将我重定向到“Microsoft Fix”。有人能用简单的话解释一下,什么是错的吗?

【问题讨论】:

  • 您是否尝试过使用 Sql Server Profiler 进行跟进?
  • 您似乎在查询引擎中遇到了错误情况(即访问冲突...)。您应该在实例的日志目录中创建一个小型转储,其中包含更多信息。如果您选择使用 Microsoft PSS 开箱,他们会进行调查以查明错误的确切来源。
  • 尝试更新 CTE 是否合法?您应该尝试根据对 CTE 的查询来更新您的实际表。另外,我觉得很奇怪你再次尝试通过myTabl 加入你的 CTE 结果。
  • CTE 是可更新的。并非在所有情况下,但你会得到一个不同的错误。我认为有些东西不能修改基础表。
  • 有效;您也可以在 CTE 中更新,但为什么要加入 CTE 创建的同一张表;而是在创建 CTE 表达式时进行自联接。另外,尝试对您的表 mytable 运行 DBCC。可能是损坏的索引问题。

标签: sql-server common-table-expression


【解决方案1】:

我猜你需要更改设置线:

set ad = a.ad + ' ' + b.LongDescription

查看 SQL fiddle 演示 here

【讨论】:

  • += 是 INSERT 语句中的有效运算符。见msdn.microsoft.com/en-us//library/ms177523.aspx
  • 谢谢@qxg。就个人而言,我不喜欢它,因为有时很难理解那里的逻辑。如果你将它拆分为set ad = ad + ' ' + b.LongDescription,你会看到另一个错误,这就是为什么我建议将它们吐出来并为该行使用别名。
【解决方案2】:

试试这个........

;With CTE as
(
select
    a.rn,
    a.LongDescription as ad,
    b.LongDescription as bd
from myTabl as a
    left join myTabl as b on a.rn +1 = b.rn
where
    a.rn=1
and
    a.LongDescription = 'Eric'
)
update CTE
    set ad += ' ' + b.LongDescription
from CTE as a
    left join 
    ( select (rn+1) AS NewRN,* from myTabl )as b on a.rn = b.NewRN
where
    a.rn=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多