如何使用 RAISEERROR 语句?
Posted
技术标签:
【中文标题】如何使用 RAISEERROR 语句?【英文标题】:How to use RAISEERROR statement? 【发布时间】:2013-02-25 18:56:18 【问题描述】:这是非常基本的东西,但我看不懂,手册也没有帮助:
declare @rule int =
(select id from menu_availability_rules
where (daily_serving_start = null or
(daily_serving_start is null and null is null)) and
(daily_serving_end = null or
(daily_serving_end is null and null is null)) and
(weekly_service_off = 3 or
(weekly_service_off is null and 3 is null)) and
(one_time_service_off = null or
(one_time_service_off is null and null is null)));
print @rule;
-- syntax error here --\/
if (@rule is not null) raiseerror ('test error', 42, 42);
if @rule is not null
begin
delete from menu_availability
where menu_id = 5365 and rule_id = @rule
delete from menu_availability_rules
where (daily_serving_start = null or
(daily_serving_start is null and null is null)) and
(daily_serving_end = null or
(daily_serving_end is null and null is null)) and
(weekly_service_off = 3 or
(weekly_service_off is null and 3 is null)) and
(one_time_service_off = null or
(one_time_service_off is null and null is null))
and not exists
(select rule_id from menu_availability
where rule_id = @rule)
end
为什么是语法错误?我该怎么写?为了调试目的,我需要抛出错误,只是为了确保代码到达条件分支。
我可以把raiseerror
替换成select 1 / 0
就可以得到我需要的东西,但是为什么我不能正常做呢?
【问题讨论】:
由于某种原因,命令拼写为raiserror
,而不是raiseerror
。
【参考方案1】:
正确的名字是RAISERROR
。
【讨论】:
同一个命令名中的两个连续的E属于不同的词,他们可能受不了了。以上是关于如何使用 RAISEERROR 语句?的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server Try/Catch、Begin/Tran、RaiseError、Loop 和 Return。正确的顺序是啥?