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:

  1. you can construct own error message, in exception.message
  2. error_number worthless
  3. error_severity severity different purposes
  4. hey, there error_state, can use :)
  5. 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

Popular posts from this blog

c++ - Function signature as a function template parameter -

How to call a javascript function after the page loads with a chrome extension? -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -