使用 RAISERROR 在 SQL Server 2005 中不起作用? [英] Using RAISERROR isn't working in SQL Server 2005?

查看:31
本文介绍了使用 RAISERROR 在 SQL Server 2005 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

BEGIN TRY 

    BEGIN TRANSACTION
       -- DO SOMETHIING

    COMMIT TRAN

END TRY
BEGIN CATCH
   IF(@@TRANCOUNT > 0)
     ROLLBACK TRANSACTION

   RAISERROR(ERROR_MESSAGE(), ERROR_SEVERITY(), ERROR_STATE()) --ERROR: Incorrect syntax near 'ERROR_MESSAGE'.

END CATCH

但是,RAISERROR 语句不起作用.raise 错误语句有什么问题?

However, the RAISERROR statement isn't working. What is wrong in the raise error statement?

推荐答案

RAISERROR 遵循与任何其他存储过程调用相同的规则.传入的参数必须是常量或变量.您不能直接将函数作为参数传递.有关这方面的文档,请参阅执行存储过程.

RAISERROR follows the same rules as any other stored procedure call. Parameters passed in must be a constant or a variable. You cannot pass a function directly as a parameter. See Executing Stored Procedures for documentation on this.

/* Demo Code - Functions accept functions as parameters
               while stored procedures do not              */

create function dbo.fnDayOfWeek 
    (@date datetime) 
    returns int
as
begin
    declare @x int
    set @x = DATEPART(day,@date)
    return (@x)
end
go

/* Both statements are successful */
select dbo.fnDayOfWeek('2010-08-06')
go
select dbo.fnDayOfWeek(GETDATE())
go

drop function dbo.fnDayOfWeek
go

create procedure DayOfWeek
    @date datetime
as
begin
    select DATEPART(day,@date)
end
go

/* First call succeeds, second fails */
exec DayOfWeek @date = '2010-08-06'
go
exec DayOfWeek @date = getdate()
go

drop procedure DayOfWeek
go

这篇关于使用 RAISERROR 在 SQL Server 2005 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆