如何使用RAISEERROR语句?

时间:2013-03-10 16:06:31

标签: tsql raiseerror

这是非常基本的东西,但我无法理解,手册没有帮助:

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,我会得到我需要的东西,但为什么我不能正常做到?

1 个答案:

答案 0 :(得分:2)

正确的名称是RAISERROR