I have the following code below:
我有以下代码:
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
However, the RAISERROR statement isn't working. What is wrong in the raise error statement?
但是,RAISERROR语句不起作用。提出错误声明有什么问题?
2 个解决方案
#1
8
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.
RAISERROR遵循与任何其他存储过程调用相同的规则。传入的参数必须是常量或变量。您不能直接将函数作为参数传递。有关此文档,请参阅执行存储过程。
/* 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
#2
14
Error is occurring because you directly using function in RaiseError
so to avoid this try the below code Try this is working for me
发生错误是因为您直接在RaiseError中使用函数,所以为了避免这种尝试下面的代码尝试这对我有用
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH;
#1
8
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.
RAISERROR遵循与任何其他存储过程调用相同的规则。传入的参数必须是常量或变量。您不能直接将函数作为参数传递。有关此文档,请参阅执行存储过程。
/* 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
#2
14
Error is occurring because you directly using function in RaiseError
so to avoid this try the below code Try this is working for me
发生错误是因为您直接在RaiseError中使用函数,所以为了避免这种尝试下面的代码尝试这对我有用
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text.
@ErrorSeverity, -- Severity.
@ErrorState -- State.
);
END CATCH;