delphi - Show error message that is raised from SQL Server -
i use "adoconnection" connect sql server , use below code in adoquery :
begin try ... end try begin catch raiserror(ltrim(str(error_number()))) end catch
when call error message : "adoconnection.errors[0].nativeerror" returns "5000"
and when call error message : "adoconnection.errors[0].number" returns negative number!
how can right error number sql server?
all own errors mssql have error_number 50000. can register own messages using sp_addmessage, using them clumsy.
mssql 2012 have new keyword throw, want.
so can on 2008, assuming recover failure in catch block:
- you can construct own error message, in exception.message
- error_number worthless
- error_severity severity different purposes
- hey, there error_state, can use :)
- for stored procedures, can use return value or output parameter passing information success or failure
begin try begin tran -- -- code -- if @@rowcount = 0 or @someyourvalue <> @someothervalue raiserror('your own error message', 16, 1); -- -- code -- if @@rowcount > 10 or @someyourvalue = @someothervalue raiserror('your own error message', 16, 2); commit end try begin catch declare @errormessage nvarchar(2048) = error_message(), @errornumber int = error_number(), @errorseverity int = error_severity(), @errorstate int = error_state(), @errorprocedure nvarchar(126) = error_procedure(), @errorline int = error_line() rollback -- construct own awesome message set @errormessage = left( 'error ' + cast(@errornumber varchar(10)) + ': ' + @errormessage + ' line: ' + cast(@errorline varchar(10)) + isnull(' ' + @errorprocedure, ''), 2044) -- pass error raiserror(@errormessage, @errorseverity, @errorstate) -- or use return value if want numbers instead error messages sql if @errornumber < 50000 return @errornumber else return -@errorstate -- in delphi: adoproc.parameters.parambyname('@return_value').asinteger: -- 0 = ok -- > 0 - sql error number -- < 0 - hey, own errors! -- or declare @errormessage, @errornumber , @errorstate -- procedure's output parameters end catch
Comments
Post a Comment