Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a message in error handling with try, throw and catch

I want to print a message ("The table employe is not exist in database") in my query.

My query is:

create proc sp_emprecord
as 
begin
    select * from employe
end

begin try
    execute sp_emprecord
end try

begin catch
    select
    error_message() as errormessage,
    error_number() as erronumber,
    error_state() as errorstate,
    error_procedure() as errorprocedure,
    error_line() as errorline;
 end catch
like image 936
user1621597 Avatar asked Jan 15 '23 15:01

user1621597


1 Answers

Try this:

create proc sp_emprecord
as 
begin
    select * from employe
end
go
begin try
    execute sp_emprecord
end try

begin catch
    if(ERROR_NUMBER() = 208)
        RAISERROR ('The table employe is not exist in database', 0, 1) WITH NOWAIT;
    else
        select
        error_message() as errormessage,
        error_number() as erronumber,
        error_state() as errorstate,
        error_procedure() as errorprocedure,
        error_line() as errorline;
 end catch
like image 126
Jānis Avatar answered Jan 28 '23 15:01

Jānis