Insql retrieval fatal error

I am trying to execute a INSERT statement on a mySQL DB in C#: MySqlConnection connection = new MySqlConnection("SERVER=" + _dbConnection + ";" + "DATABASE=" + _dbName + ";" + "PORT=" + _dbP...

I am trying to execute a INSERT statement on a mySQL DB in C#:

MySqlConnection connection = new MySqlConnection("SERVER=" + _dbConnection + ";" +
   "DATABASE=" + _dbName + ";" +
   "PORT=" + _dbPort + ";" +
   "UID=" + _dbUsername + ";" +
   "PASSWORD=" + _dbPassword + ";");
MySqlDataAdapter adapter;
DataSet dataset = new DataSet();
command = new MySqlCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO plugins (pluginName, enabled) VALUES (@plugin,@enabled)";
command.Parameters.AddWithValue("@name", "pluginName");
command.Parameters.AddWithValue("@enabled", false);
adapter = new MySqlDataAdapter(command);
adapter.Fill(dataset);

The plugin table consists of two columns: pluginName(varchar(50)) and enabled(boolean).

This fails with the error: mysql Fatal error encountered during command execution. Any ideas on why this would fail?

asked Mar 18, 2010 at 4:52

Brian's user avatar

You seem to be using @plugin in the query but @name in the parameters, although I’d expect a better error message if that was the sole problem!

answered Mar 18, 2010 at 4:56

Chris's user avatar

ChrisChris

10.3k1 gold badge38 silver badges46 bronze badges

2

The error «Fatal error encountered during command execution» seems to be for general syntax errors in the CommandText line.

For example, the following just tripped me up for a while:

...@date,@email@first,@last...

Watch out for those commas! There should be one after @email:

...@date,@email,@first,@last...

answered Jul 12, 2012 at 21:03

DanM7's user avatar

DanM7DanM7

2,1853 gold badges27 silver badges46 bronze badges

Missing Parameters could also be a reason.
eg:

cmd.CommandText = "INSERT INTO login VALUES(@uname,@pwd);"
cmd.Parameters.AddWithValue("@uname",TxtUsername.Text.ToString())
'forgot to place "@pwd"

cmd.ExecuteNonQuery()

You get fatal error which can be solved by adding

cmd.Parameters.AddWithValue("@pwd",TxtPassword.Text.ToString())

answered Apr 15, 2019 at 6:38

Pranav Premarajan's user avatar

`(from user in _context.GetDataTable("call sp_get_user_subscribe_info(@param1, @param2)", _context.AddParameter(new dynamic[] { id }))`

My procedure has two parameters but I have passed only one parameter that’s why this error message occurred, But this error message is so confusing.

answered Jun 3, 2022 at 6:50

Md Somrat Akbor's user avatar

Содержание

  1. KB3173976 — FIX: Fatal Error when you run a query against the sys.sysindexes view in SQL Server 2016
  2. Symptoms
  3. Resolution
  4. Error Handling in SQL 2000 – a Background
  5. SqlClient
  6. OleDb

KB3173976 — FIX: Fatal Error when you run a query against the sys.sysindexes view in SQL Server 2016

Symptoms

Assume that you create a query session to a database that contains Memory-optimized tables and table types. When you run a query against the sys.sysindexes view of the database, the query session is terminated. Additionally, you receive an error message that resembles the following:

Msg 3624, Level 20, State 1, Line 12
A system assertion check has failed. Check the SQL Server error log for details. Typically, an assertion failure is caused by a software bug or data corruption. To check for database corruption, consider running DBCC CHECKDB. If you agreed to send dumps to Microsoft during setup, a mini dump will be sent to Microsoft. An update might be available from Microsoft in the latest Service Pack or in a Hotfix from Technical Support.
Msg 596, Level 21, State 1, Line 11
Cannot continue the execution because the session is in the kill state.
Msg 0, Level 20, State 0, Line 11A severe error occurred on the current command. The results, if any, should be discarded.

Note If you run DBCC CHECKDB on the database to check for database corruption, there is no error reported.

Resolution

The issue was first fixed in the following cumulative update of SQL Server:

Each new cumulative update for SQL Server contains all the hotfixes and all the security fixes that were included with the previous cumulative update. We recommend that you download and install the latest cumulative updates for SQL Server:

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the «Applies to» section.

Learn about the terminology that Microsoft uses to describe software updates.

Источник

Error Handling in SQL 2000 – a Background

An SQL text by Erland Sommarskog, SQL Server MVP. Last revision 2009-11-29.

This is one of two articles about error handling in SQL Server 2000. This article focuses on how SQL Server – and to some extent ADO – behave when an error occurs. The other article, Implementing Error Handling with Stored Procedures , gives advice for how you should check for errors when you write stored procedures. Logically, this article is part one, and Implementing. is part two. However, you can read the articles in any order, and if you are relatively new to SQL Server, I recommend that you start with Implementing. . The article here gives a deeper background and may answer more advanced users’ questions about error handling in SQL Server.

Note : this article was written for SQL 2000 and earlier versions. All I have for SQL 2005 is unfinished article with a section Jumpstart Error Handling . The content in this article is to some extent applicable to SQL 2005 as well, but you will have to use your imagination to map what I say to SQL 2005. The article includes a short section on TRY-CATCH . I hope to produce a complete article for error handling in SQL 2005 later on.

Table of Contents:

In many aspects SQL Server is a very good DBMS that permits you implement powerful solutions with good performance. However, when it comes to error handling. To be blunt: error handling in SQL Server is poor. It is a patchwork of not-always-so-consistent behaviour. It’s also weak in that you have fairly little control over error handling, and for advanced error handling like suppressing errors or logging errors, you must take help from the client-side. Unfortunately, depending on which client library you use, you may find that the client library has its own quirks, sometimes painting you into a corner where there is no real good solution.

In this article, I will first look at what parts an error message consists of, and how you can detect that an error has occurred in code. Next, I describe the possible actions can SQL Server can take in case of an error. I then proceed to describe the few possibilities you have to control SQL Server’s error handling. Finally, there is a section on how the different client libraries from Microsoft behave, with most of the focus on ADO and ADO .Net.

General disclaimer: whereas some information in this text is drawn from Books Online and other documentation from Microsoft, a lot of what I say is based on observations that I have made from working with SQL Server, and far from all of this is documented in Books Online. Therefore, you should be wary to rely on a specific behaviour like «this error have this-and-this effect», as it could be different in another version of SQL Server, even different between service packs.

Here is a typical error message you can get from SQL Server when working from Query Analyzer.

Note: Under Tools -> Options -> Connections, I have checked Parse ODBC Message Prefixes .

The error information that SQL Server passes to the client consists of several components, and the client is responsible for the final interpretation of the message. These are the components that SQL Server passes to the client.

Message number – each error message has a number. You can find most of the message numbers in the table sysmessages in the master database. (There some special numbers like 0 and 50000 that do not appear there.) In this example, the message number is 547. Since most interesting messages are errors, I will also use the term error number . Message numbers from 50001 and up are user-defined. Lower numbers are system defined.

Severity level – a number from 0 to 25. The stort story is that if the severity level is in the range 0-10, the message is informational or a warning, and not an error. Errors resulting from programming errors in your SQL code have a severity level in the range 11-16. Severity levels 17-25 indicate resource problems, hardware problems or internal problems in SQL Server, and if the severity is 20 or higher, the connection is terminated. For the long story, see the section More on Severity Levels for some interesting tidbits. For system messages you can find the severity level in master..sysmessages , but for some messages SQL Server employs a different severity level than what’s in sysmessages .

State – a value between 0 and 127. The meaning of this item is specific to the error message, but Microsoft has not documented these values, so this value is rarely of interest to you.

Procedure – in which stored procedure, trigger or user-defined function the error occurred. Blank if the error occurred in a plain batch of SQL statements (including dynamic SQL ).

Line – Line number within the procedure/function/trigger/batch the error occurred. A line number of 0 indicates that the problem occurred when the procedure was invoked.

Message text – the actual text of the message that tells you what went wrong. You can find this text in master..sysmessages, or rather a template for it, with placeholders for names of databases, tables etc.

As I mentioned the client is responsible for the formatting of the error message, and for messages with a severity level with 10 or lower, most client programs print only the message text, but not severity level, procedure etc. In fact, we see an example of this above. The text The statement has been terminated is a message on its own, message 3621.

When you write your own client program, you can choose your own way to display error messages. You may be somewhat constrained by what your client library supplies to you. The full information is available with low-level interfaces such as DB-Library, ODBC or the OLE DB provider for SQL Server. On the other hand, in ADO you only have access to the error number and the text of the message.

There are two ways an error message can appear: 1) an SQL statement can result in an error (or a warning) 2) you emit it yourself with RAISERROR (or PRINT ). Let’s take a brief look at RAISERROR here. Here is sample statement:

Here you supply the message text, the severity level and the state. The output is:

Thus, SQL Server supplies the message number 50000, which is the error number you get when you supply a text string to RAISERROR . (There is no procedure name here, since I ran the statement directly from Query Analyzer.) Rather than a string, you could have a supplied a number of 50001 or greater, and SQL Server would have looked up that number in sysmessages to find the message text. You would have stored that message with the system procedure sp_addmessage . (If you just supply a random number, you will get an error message, saying that the message is missing.) Whichever method you use, the message can include placeholders, and you can provide values for these placeholders as parameters to RAISERROR , something I do not cover here. Please refer to Books Online for details.

As I mentioned State is rarely of interest. With RAISERROR , you can use it as you wish. If you raise the same message in several places, you can provide different values to State so that you can conclude which RAISERROR statement that fired. The command-line tools OSQL and ISQL have a special handling of state: if you use a state of 127, the two tools abort and set the DOS variable ERRORLEVEL to the message number. This can be handy in installation scripts if you want to abort the script if you detect some serious condition. (For instance, that database is not on the level that the installation script is written for.) This behaviour is entirely client-dependent; for instance, Query Analyzer does not react on state 127.

After each statement in , with one single exception that I cover in the next section, SQL Server sets the global variable @@error to 0, unless an error occurs, in which case @@error is set to the number of that error. (Note: these days, the SQL Server documentation refers to @@error as a «function». Being an old-timer, I prefer «global variables» for the entities whose names that start with @@.)

More precisely, if SQL Server emits a message with a severity of 11 or higher, @@error will hold the number of that message. And if SQL Server emits a message with a severity level of 10 or lower, SQL Server does not set @@error, and thus you cannot tell from that the message was produced.

But the message number is also the only field of the error message that you easily can access from . A common question on the newsgroups is how to retrieve the text of an error message, and for a long time the answer was «you can’t». But Mark Williams pointed out to me a way to do it. However it requires that the user to have sysadmin privileges, so you cannot easily use it in an application. I will return to this topic in the section Retrieving the Text of an Error Message .

There is no way to prevent SQL Server from raising error messages. There is a small set of conditions for which you can use SET commands to control whether these conditions are errors or not. We will look closer at these possibilities later, but I repeat that this is a small set, and there is no general way in to suppress error messages. You will need to take care of that in your client code. (Another common question on the newsgroups.)

As I mentioned, @@error is set after each statement. Therefore, you should always save the save the value of @@error into a local variable, before you do anything with it. Here is an example of what happens if you don’t:

Here is the correct way.

All stored procedures have a return value, determined by the RETURN statement. The RETURN statement takes one optional argument, which should be a numeric value. If you say RETURN without providing a value, the return value is 0 if there is no error during execution. If an error occurs during execution of the procedure, the return value may be 0, or it may be a negative number. The same is true if there is no RETURN statement at all in the procedure: the return value may be a negative number or it may be 0.

Whether these negative numbers have any meaning, is a bit difficult to tell. It used to be the case, that the return values -1 to -99 were reserved for system-generated return values, and Books Online for earlier versions of SQL Server specified meanings for values -1 to -14. However, Books Online for SQL 2000 is silent on any such reservations, and does not explain what -1 to -14 would mean.

With some occasional exception, the system stored procedures that Microsoft ships with SQL Server return 0 to indicate success and any non-zero value indicates failure.

While there is no law that requires you to follow the same convention for your stored procedures, my strong recommendation is that you use return values solely to indicate success/failure. If you want to return data such as the id for an inserted row, number of affected rows or whatever, use an OUTPUT parameter instead. It follows from the fact that a blank RETURN may return 0, even if there has been an error during execution, that you should be careful to return an explict value yourself if an error occurs in the procedure.

There is one situation when a stored procedure does not return any value at all, leaving the variable receiving the return value unaffected. This is when the procedure is aborted because of a scope-aborting error. We will look more into this later. There is also one situation when the return value is NULL : this happens with remote procedures and occurs when the batch is aborted on the remote server. (Batch-abortion is also something we will look into more later on.)

There is one curious exception to the rule that @@error is set after each statement: a RETURN without parameters does not change the value of @@error, but leaves the variable unchanged. In my opinion, this is not really practically useful. (I owe this information to a correspondent who gave me this tip by e-mail. Alas, I lost his mail due to problems at my ISP, so I can credit him by name.)

@@rowcount is a global variable reports the number of affected rows in the most recently executed statement. Just like @@error you need to save it in a local variable if you want to use the value later, since @@rowcount is set after each statement. Since with SET you can only assign variable at a time, you must use SELECT if you need to save both @@error and @@rowcount into local variables:

(For this reason, I prefer to always use SELECT for variable assignment, despite Microsoft’s recommendations to use SET.)

In it is not an error if, for instance, an UPDATE statement did not affect any rows. But it can of course indicate an error in your application, as it could be an error if a SELECT returns more that one row. For these situations, you can check @@rowcount and raise an error and set a return value, if @@rowcount is not the expected value.

@@trancount is a global variable which reflects the level of nested transactions. Each BEGIN TRANSACTION increases @@trancount by 1, and each COMMIT TRANSACTION decreases @@trancount by 1. Nothing is actually committed until @@trancount reaches 0. ROLLBACK TRANSACTION rolls back everything to the outermost BEGIN TRANSACTION (unless you have used the fairly exotic SAVE TRANSACTION ), and forces @@trancount to 0, regards of the previous value.

When you exit a stored procedure, if @@trancount does not have the same value as it had when the procedure commenced execution, SQL Server raises error 266. This error is not raised, though, if the procedure is called from a trigger, directly or indirectly. Neither is it raised if you are running with SET IMPLICIT TRANSACTIONS ON .

In this section we will look a little closer on the various severity levels.

Messages with Level 0 are purely informational. A PRINT statement produces a message on severity level 0. These messages do not set @@error. Most query tools prints only the text part of a level 0 message.
These levels, too, are for informational messages/warnings. I cannot recall that I have encountered this from SQL Server, but I’ve used it myself in RAISERROR at times. Query Analyzer and SQL Management Studio prints the message number, the level and the state, but not the procedure and line number for these messages.
10 This level does not really exist. It appears that SQL Server internally converts level 10 to level 0, both for its own messages when you use level 10 in RAISERROR .
These levels indicate a regular programming error of some sort. But it is not the case that level 16 is more serious than level 11. Rather it appears to be a somewhat random categorisation. Books Online gives no details on what the levels might mean, but SQL Server MVP Jacco Schalkwijk pointed out to me that there is a drop-down box in the dialog for defining alerts in Enterprise Manager and SQL Management Studio which has description on these levels. Here is what the drop-down box has to say:

11 – Specified Database Object Not Found
12 – Unused
13 – User Transaction Syntax Error
14 – Insufficient Permission
15 – Syntax Error in SQL Statements
16 – Miscellaneous User Error

My experience is that it may not always be this way, but there certain are matches. Deadlock, for instance is level 13. (So now you know what a User Transaction Syntax Error is!)

17-25 Messages with any of these severity levels indicate some sort of resource problem (for instance running out of disk space), or internal error in SQL Server, or a problem with the operating system or hardware. The higher the severity, the more serious problems. These levels are documented in in the setion Troubleshooting Error Messages Error Message Formats Error Message Severity Levels in Books Online.
19-25 To use level 19 or higher in RAISERROR you must use the WITH LOG option, and you must have sysadmin rights.
20-25 Errors with these severity levels are so fatal, that they always terminate the connection.

Many programming languages have a fairly consistent behaviour when there is a run-time error. Common is that the execution simply terminates in case of an error, unless you have set up an exception handler that takes care the error. In other languages, some error variable is set and you have to check this variable. is confusing, because depending on what error that occurs and in which context it occurs, SQL Server can take no less than four different actions. I first give an overview of these alternatives, followed by a more detailed discussion of which errors that cause which actions. I then discuss two special cases: trigger context and user-defined functions.

These are the four main possible actions SQL Server can take:

Statement-termination . The current statement is aborted and rolled back. Execution continues on the next statement. Any open transaction is not rolled back. @@error is set to the number of the error. Since the statement is rolled back, this means that if you run an UPDATE statement that affects 1000 rows, and for one row a CHECK constraint is violated, none of the rows will be updated. But if the UPDATE statement was part of a longer transaction, the effect of the preceding INSERT, UPDATE or DELETE statements are not affected. You need to issue a ROLLBACK TRANSACTION yourself to undo them.

Scope-abortion . The current scope (stored procedure, user-defined function, or block of loose SQL statements, including dynamic SQL ) is aborted, and execution continues on the next statement in the calling scope. That is, if stored procedure A calls B and B runs into a scope-aborting error, execution continues in A, just after the call to B. @@error is set, but the aborted procedure does not have a return value, but the variable to receive the return value is unaffected. As for statement-termination, any outstanding transaction is not affected, not even if it was started by the aborted procedure.

Batch-abortion. The execution of the entire batch – that is, the block of SQL statements that the client submitted to SQL Server – is aborted. Any open transaction is rolled back. @@error is still set, so if you would retrieve @@error first in the next batch, you would see a non-zero value. There is no way you can intercept batch-abortion in code. (Almost. We will look a possibility using linked servers later on.)

Connection-termination . The client is disconnected and any open transaction is rolled back. In this case there is no @@error to access.

One can note from this, that there are two things that cannot happen:

  • The transaction is rolled back, but execution of the current batch continues.
  • The batch is aborted, but the transaction is not rolled back.

But I like to stress that this is based on my own observations. I have found no documentation that actually states that these two cases cannot occur under any circumstances.

The above caters for most of the error situations in SQL Server, but since a hallmark of the error handling in SQL Server is inconsistency, every now and then I discover some new odd situation. I am overlooking these cases here, not to burden the reader with too many nitty-gritty details.

There is however, one more situation you should be aware of and that is batch-cancellation . The client may at any time tell SQL Server to stop executing the batch, and SQL Server will comply more or less immediately. In this situation SQL Server will not roll back any open transaction. (In the general case that is. It seems that if the execution is in a trigger, when the cancellation request comes, then there is a rollback.) However, if the current statement when the cancellation request comes in is an UPDATE, INSERT or DELETE statement, then SQL Server will roll back the updates from that particular statement. Batch-cancellation may occur because an explicit call to a cancellation method in the client code, but the most common reason is that a query timeout in the client library expires. ODBC, OLE DB, ADO and ADO .Net all have a default timeout of 30 seconds. (Which judging from the questions on the newsgroups, many programmers believe to come from SQL Server, but not so.)

As you may guess, it depends on the error which action SQL Server takes, but not only. Context also matters. One is the setting of the command SET XACT_ABORT , which we shall look at in a later section. A special case is trigger context , in which almost all errors abort the batch and this will be the topic for the next section. Right now we will discuss the default context, that is outside triggers and when the setting XACT_ABORT is OFF .

You may guess that the more severe the error is, the more drastic action SQL Server takes, but this is only really true for connection-termination. When it comes to scope-abortion, this occurs for a fairly well-defined family, but I am not sure that I agree with that these errors are less severe than the errors that abort the batch. And there is not really any clear distinction between the errors that abort the batch on the one hand, and those that merely terminate the statement on the other. For this reason, I will first cover connection-termination, then scope-abortion and then the other two together.

When SQL Server terminates the connection, this is because something really bad happened. The most common reason is an execution error in the SQL Server process itself, e.g. an access violation (that is, attempt to access an illegal memory address), a stack overflow, or an assertion error (a programmer-added check for a certain condition that must be true for his code to work). It could also be a protocol error in the communication between the client library and SQL Server. These errors are normally due to bugs in SQL Server or in the client library, but they can also appear due to hardware problems, network problems, database corruption or severe resource problems.

SQL Server terminates the connection, because it would not be safe to continue execution, as internal process structures may be damaged. In some cases, not only is your connection terminated, but SQL Server as such crashes.

Connection-termination can sometimes be due to errors in your application in so far that you may have written some bad SQL that SQL Server could not cope with. But in such case it is still an SQL Server bug if the connection terminates, because you should get a proper error message. (The error messages in conjunction with connection-termination are often very opaque.)

There is one case, though, where a bug in application code can cause connection-termination on its own, and that is if you have your written your own extended stored procedures or your own OLE objects that you call through the sp_OAxxxxx procedures. An unhandled execution error in such code will terminate your connection – and may crash SQL Server as well.

There is one way to terminate the connection from : if you issue a RAISERROR statement with a severity level >= 20. To do this you must provide WITH LOG , and you must be sysadmin . Since errors with severities >= 19 may trigger an operator alert, and eventually may alert someone’s pager, don’t do this just for fun.

This appears to be confined to compilation errors. At least I have not seen it happen with any other sort of error. Due to the feature known as deferred name resolution (in my opinion this is a misfeature), compilation errors can happen during run-time too. Consider this example (you can run it in the Northwind database):

Because the table #temp does not exist when you create inner_sp , SQL Server defers examination of the entire INSERT-SELECT statement until run-time. Again, when you invoke inner_sp , SQL Server cannot find #temp and defers building a query plan for the INSERT-SELECT statement until it actually comes to execute the statement. It is first at this point, that SQL Server discovers that the SELECT statement is incorrect (the alias for Orders is missing). And at that precise point, the execution of inner_sp is aborted. Here is the output:

Note the next-to-last line in the output: inner_sp started a transaction. But just because inner_sp was aborted does not mean that the transaction was rolled back. When you implement you error handling, this is something you need to consider, and I look closer at this in the accompanying article on error handling.

Also observe that @ret never was set, but retained the value it had prior to the call.

Not all compilation errors passes unnoticed when SQL Server loads a procedure. A pure syntax error like a missing parenthesis will be reported when you try to create the procedure. But the list of errors not detected because of deferred name resolution is longer than you might expect. After all, one would expect SQL Server be able to detect the missing alias even if #temp is missing. With some effort, it could even detect the missing alias with the Orders table missing, couldn’t it?

Actually, I can offer a way to avoid this problem altogether. My toolset AbaPerls, offerde as freeware that includes a load tool, ABASQL. Before creating a procedure, ABASQL extracts all temp tables in the procedure and creates them, so that SQL Server will flag errors such as missing aliases or columns. ABASQL also checks the SQL code for references to non-existing tables.

These two groups comprise regular run-time errors, such as duplicates in unique indexes, running out of disk space etc. As I have already have discussed, which error that causes which action is not always easy to predict beforehand. This table lists some common errors, and whether they abort the current statement or the entire batch.

Error Aborts
Duplicate primary key. Statement
NOT NULL violation. Statement
Violation of CHECK or FOREIGN KEY constraint. Statement
Most conversion errors, for instance conversion of non-numeric string to a numeric value. BATCH
Attempt to execute non-existing stored procedure. Statement
Missing or superfluous parameter to stored procedure to a procedure with parameters. Statement
Superfluous parameter to a parameterless stored procedure. BATCH
Exceeding the maximum nesting-level of stored procedures, triggers and functions. BATCH
Being selected as a deadlock victim. BATCH
Permission denied to table or stored procedure. Statement
ROLLBACK or COMMIT without any active transaction. Statement
Mismatch in number of columns in INSERT-EXEC . BATCH
Declaration of an existing cursor Statement
Column mismatch between cursor declaration and FETCH statement. Statement.
Running out of space for data file or transaction log. BATCH

I am only able to make out a semi-consistency. Some real fatal errors after which I would not really be interested in continuing execution do abort the batch. The examples here are deadlock victim and running out of disk space. But why would it be more severe to pass a superfluous parameter to a parameterless one, than to one that has parameters? And conversion errors? Are they more severe than a constraint violation? And why not all conversion errors? (We will return to conversion errors, as well as arithmetic errors that I purposely excluded from this table, when we discuss the SET commands ANSI_WARNINGS and ARITHABORT . They belong to the small et of errors, where you have some sort of a choice.)

And don’t look to severity levels for help. As noteed above, the severity levels 11-16 is another classification, that don’t reflect any difference in severity. Most of the errors above have severity level 16, but being a deadlock victim has severity level 13. (Running out of a disk space, which is a resource problem, is level 17.)

You have trigger context when you are in a trigger, or you are in a stored procedure, user-defined function or block of dynamic SQL that has been called directly or indirectly from a trigger. That is, somewhere on the call stack, there is a trigger. If you are in trigger context, all errors terminate the batch and roll back the transaction on the spot. (Connection-terminating errors still terminate the connection, of course.)

Well, almost. When it comes to error handling in SQL Server, no rule is valid without an exception. Errors you raise yourself with RAISERROR do not abort the batch, not even in trigger context. Neither does error 266, Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing . This error is simply not raised at all when this condition occurs in trigger context. No, this is not a bug, but it is documented in Books Online, and according to Books Online, error 266 is informational only. (Now, taste that concept: an informational error.)

There is one more way that a trigger can terminate the batch. This happens if @@trancount is 0 when the trigger exits. A trigger always executes in the context of a transaction, since even if there is no multi-statement transaction in progress each INSERT, UPDATE and DELETE statement is its own transaction in SQL Server, and the trigger is part of that transaction. Thus, @@trancount is at least 1 when you enter a trigger, and if it is 0 on exit this means that somewhere has been a ROLLBACK statement. (Or sufficiently many COMMIT to bring @@trancount to 0.) Why this would have to abort the batch? Because the sky is blue. Seriously, I don’t know, but it has always been that way, and there is no way you can change it.

The normal use for this is that if you have an integrity check in a trigger you raise a message and roll back the transaction, as in this example.

Thus, this trigger aborts the batch, not because of the RAISERROR , but because of the ROLLBACK TRANSACTION , and since the trigger is permitted to execute to end, the RAISERROR statement is executed.

User-defined functions are usually invoked as part of a SET, SELECT, INSERT, UPDATE or DELETE statement. What I have found is that if an error appears in a multi-statement table-valued function or in a scalar function, the execution of the function is aborted immediately, and so is the statement the function is part of. Execution continues on the next line, unless the error aborted the batch. In either case, @@error is 0 . Thus, there is no way to detect that an error occurred in a function from .

The problem does not appear with inline table-functions, since an inline table-valued function is basically a macro that the query processor pastes into the query.

You can also execute scalar functions with the EXEC statement. In this case, execution continues if an error occurs (unless it is a batch-aborting error). @@error is set, and you can check the value of @@error within the function. It can be problematic to communicate the error to the caller though.

No, SQL Server does not offer much in this area, but we will look at the few possibilities, of which the most important is SET XACT_ABORT ON .

What I have said this far applies to when XACT_ABORT is OFF , which is the default. When you issue SET XACT_ABORT ON , the very most of the statement-terminating errors instead become batch-aborting errors. Thus, if you don’t want to litter your code with checks on @@error, and if you are not interested in trying to recover from the error or invoke some error-logging routine in , but you are content with execution being aborted on first error, then XACT_ABORT is for you.

Beware, though, that even when XACT_ABORT is ON , not all errors terminate the batch. Here are the exceptions I know of:

  • Errors you raise yourself with RAISERROR .
  • Compilation errors (which normally terminate the scope) do not terminate the batch.
  • Error 266, Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing .

So at a minimum you still need to check @@error after the execution of a stored procedure or a block of dynamic SQL even if you use XACT_ABORT ON .

These three SET commands give you very fine-grained control for a very small set of errors. When a division by zero or an overflow occurs, there are no less four choices.

  • No action at all, result is NULL – when ARITHIGNORE is ON .
  • Warning message, result is NULL – when all are OFF .
  • Statement-termination – when ANSI_WARNINGS is ON .
  • Batch-abortion – when ARITHABORT is ON and ANSI_WARNINGS is OFF .

ARITHABORT and ARITHIGNORE also control domain errors, such as attempt to take the square root of a negative number. But this is error is not covered by ANSI_WARNINGS , so here you only have three choices.

As for what is an overflow, SQL Server has extended the domain of this error to datetime value in a way which is not really intuitive. Consider these two statements:

Thus, if you have a string which conforms syntactically to some date format, but some element is out of range, this particular form of conversion error only aborts the batch with a certain setting – and in other settings it may not cause an error at all.

ANSI_WARNINGS controls a few more errors and warnings. With ANSI_WARNINGS ON , it is an error to assign a character or binary column a value that exceeds the the maximum length of the column, and this terminates the statement. When ANSI_WARNINGS is OFF , this condition is not an error, but the value is silently truncated. The error is never raised for variable assignment. Also, with ANSI_WARNINGS ON , if an aggregate function such as or sees a NULL value, you get a warning message. (Thus it does not set @@error, nor terminate the statement.)

When you use ODBC, OLE DB and Query Analyzer ( SQL 2000), ANSI_WARNINGS is ON by default. Since some features (indexed views, index on computed columns and distributed queries) in SQL Server requires ANSI_WARNINGS to be ON , I strongly recommend that you stick to this. Indexed views and index on computed columns also require ARITHABORT to be ON , but I don’t think you can rely on it being ON by default.

Finally, I should mention that there is one more SET command in this area: NUMERIC_ROUNDABORT . When ON , the batch is aborted if operation with a decimal data type results in loss of precision. The option is OFF by default, and it must be OFF for indexed views and indexes on computed columns to work.

SQL Server buffers the output, so an error message or a result set may not appear directly at the client. In many cases, this is not an issue, but if you are running a long-running procedure, you may want to produce diagnostic messages. To have them displayed immediately in the client, you can use the WITH NOWAIT clause to the RAISERROR statement, as in this example:

Once there is a message with NOWAIT , all that is ahead of the message in the buffer is also passed to the client.

Unfortunately, there is a bug in SQL Server with NOWAIT , which affects you only if you are calling a procedure through RPC (remote procedure call), so that it this case, SQL Server buffers the messages nevertheless. RPC is the normal way to call a procedure from an application (at least it should be), but if you are running a script from OSQL or Query Analyzer, this bug does not affect you.

Normally when you try to insert a value that would be a duplicate in a unique index, this is an error and the statement is rolled back. However, the syntax for the CREATE INDEX statement includes the option IGNORE_DUP_KEY . When this option is in effect, duplicates are merely discarded. The statement is not rolled back, and if the INSERT statement compassed several rows, the rows that do not violate the uniqueness of the index are inserted.

According to Books Online, SQL Server issues a warning when ignoring a duplicate row. However, in real life the message has severity level 16, and thus comes across to the client as an error. Nevertheless, SQL Server does not set @@error, and as I noted the statement is not rolled back, this message falls in none of four categories I have presented. Microsoft has acknowledged the incorrect severity level as a bug, so hopefully this will be fixed in some future version of SQL Server.

This option applies to unique indexes only. It is not available for PRIMARY KEY or UNIQUE constraints.

There is no way to switch off batch-abortion on a general level. But there is actually one way to handle the case in , and that is through linked servers. If you call a remote stored procedure, and the procedure runs into a batch-aborting error, the batch in the calling server is not aborted. On return to the local server, @@error holds the value of the error that aborted the batch on the remote server, and the return value of the stored procedure is set to NULL . (At least my tests indicate this. Thus, it is not the same case as when a local procedure dies with scope-abortion, when the return value is not set at all.) It goes without saying, that this is a fairly lame workaround that is only applicable in special situations.

  • It must be a truly remote server. If you call a procedure in the local server with four-part notation, SQL Server is too smart for you.
  • Set up the remote server with SQLOLEDB . When I set up the remote server with the OLE DB -over- ODBC provider ( MSDASQL ), the diagnostics about the error was poorer on the calling server.

There is no supported way to retrieve the full text of an error message in SQL 2000. You can get a text from master.dbo.sysmessages , but then you only get placeholders for interesting things like which constraint that was violated. To get the full text of the error message in a proper way, you need a client to pick it up and log it.

However, Mark Williams pointed out you can retrieve the full mesage text from within with help of DBCC OUTPUTBUFFER . To wit, after an error has been raised, the messge text is in the output buffer for the process.

The output from DBCC OUTPUTBUFFER is a single colunm, where each row as a byte number, a list of hex values, and a textual representation of the hex values. Mark made the effort to extract the message from the last part, and was kind to send me a stored procedure he had written. As I looked at the output from DBCC OUTPUTBUFFER , I found a byte that appeared to hold the length of the message, which helped me to improve Mark’s procedure.

Much later I was contacted by Paulo Santos who looked even deeper into the output from DBCC OUTPUTBUFFER and he was able to significantly improve the procedure, and dig out not only the error message, but also severity, procedure and that. My testing shows that it is still not perfect. Sometimes one of several messages are dropped, junk characters appear and not all line numbers reported correctly. But it is far better than nothing at all and you should not expect something which relies on undocumented behaviour to be perfect. (Of course, on SQL 2005 you would use TRY-CATCH and call in your CATCH handler.)

There is a very significant restriction with this trick: to run DBCC OUTPUTBUFFER you need sysadmin rights even to look at your own spid, so you cannot put this in an application that is to be run by plain users.

The prime source for the stored procedure is at Paulo’s web site, where you find the code and some background. In case his site is down or unavailable, you can find a copy of his spGET_LastErrorMessage here as well. (But check his site first, as he may have updates). If you are curious in history, you can also look the original showErrorMessage that Mark and I produced.

Next version of SQL Server, SQL 2005, code-named Yukon, introduces significant improvements to the error handling in SQL Server. Here is a simple example:

The construct is similar to error-handling concepts in languages like C++. If an error occurs in the TRY block, or in a stored procedure called by the TRY block, execution is transferred to the CATCH block. In the CATCH block, you have access to six new functions: , , , , and , that gives you all parts of the message associated with the error. And, yes, , is the expanded message with the parameters filled in.

If you are in a transaction, and the error occurred is a batch-abortion error, your transaction will be doomed . This means that you cannot commit or perform any more updates within the transaction – you must roll back.

One caveat is that if you catch an error in this way, the client will never see the error, unless you call RAISERROR in the error handler. Unfortunately, you cannot reraise the exact error message, since RAISERROR does not permit you to use error numbers less than 50000.

The various client libraries from which you can access SQL Server have their quirks too. Some libraries are low-level libraries like DB-Library, ODBC and the SQLOLEDB provider. Others are higher-level libraries that sit on top of one of the low-level libraries, one example is ADO . If the low-level library has some quirk or limitation, the high-level library is likely to inherit that. The high-level library might also add its own quirks and limitations.

I am covering four libraries here: DB-Library, ODBC, ADO and ADO .Net, although the first two I discuss very briefly, since most devleopers today use ADO or ADO .Net.

When it comes to error handling, DB-Library is probably the best in the game. When SQL Server produces a message – be that an error, a warning or just an informational message such as a PRINT statement – DB-Library invokes a callback routine, and in that callback routine you have full access to all parts of the message: error number, severity level, state, procedure, line number and of course the message itself. You can then set some global variable to determine what should happen when you come back from the DB-Library call that caused the error.

Unfortunately, Microsoft stopped developing DB-Library with SQL 6.5, and you have poor or no support for new features in SQL Server with DB-Library. Thus, I cannot but discourage you from using DB-Library.

With ODBC , you have to rely on return-status values, and then retrieve the error message yourself. Exactly how, I have to admit that I am bit foggy on at this point. However, you do have access to all parts of the error message, and you get all messages. This is evidenced by the fact that you get all this information in Query Analyzer which connects through ODBC .

ADO is not that good when it comes to error handling. First, you don’t have full access to the error message. You only get the error number and the error text. You do not get the severity level (so you don’t know whether really is an error at all), nor do you get state, procedure or line number. You do get something called SQLState, which is a five-letter code, not related to SQL Server but inherited from ODBC . Another problem is that you do far from always get all error messages, as I will detail below.

The basic operation with ADO appears simple: You submit a command to SQL Server and if there is an error in the execution, ADO raises an error, and if you have set up an error handler with On Error Goto , this is where you will wind up. ( On Error is for Basic-derived languages. In C++ I suppose you can use try — catch , but I have not verified this.) You can retrieve all messages from SQL Server in the Errors collection on the Connection object.

But there are quite some surprises hiding here. One thing that makes ADO complicated, is that there are so many ways that you can submit a command and retrieve the results. Partly, this is due to that ADO permits you to access other data sources than SQL Server, including non-relational ones. Also, as your «command» you can simply provide a table name. Since this text is about error handling with stored procedures in SQL Server, I disregard other possibilities. But even if you want to invoke a stored procedure, there are a whole lot of choices:

  • Which provider . You can use SQLOLEDB or MSDASQL (OLE DB over ODBC ).
  • Cursor location . Server-side cursor or client-side cursor? (The concept of a cursor in this context confused me for a long time. Being an SQL programmer, I think cursors are bad and should be avoided. Eventually, I have understood that a client-side cursor is not really a cursor at all. You get the entire data to the client in one go. A Server-side cursor gets the data from the server in pieces, which may or may not involve an SQL cursor, depending on the cursor type.)
  • From which object to invoke the stored procedure. You can use the .Execute method of the Connection and Command objects or the .Open method of the Recordset object.
  • Command type . You can construct an EXEC command as a string and use adCmdText . You can also use adCmdText with ODBC syntax and supply parameters through the .Parameters collection. And you can use adCmdStoredProc to supply the name of a stored procedure and use the .Parameters collection.
  • Cursor type. Cursors can be forward-only, static, dynamic or keyset.
  • Lock type . You can choose between read-only, optimistic, batch optimistic and pessimistic.

And that’s not really all.

What errors you see in your client code, depends on which combination of all these parameters you use. I developed a form, from which I could choose between these parameters, and then I played with a fairly stupid stored procedure which depending on input could cause some errors, generate some PRINT messages and produce some results sets. And there was a great difference in what I got back. When I used SQLOLEDB and client-side cursors, I did not get any of my two PRINT messages in my .Errors collection if there were no errors, whereas with SQLOLEDB and server-side cursors I got both messages. With MSDASQL , I got the first PRINT message, but not the second, no matter the cursor location.

If there were error messages, I did not always get all of them, but at least one error was communicated and an error was raised in the VB code. However, there is a gotcha here, or two depending on how you see it. The first gotcha is that if the stored procedure produces one or more recordsets before the error occurs, ADO will not raise an error until you have walked past those preceding recordsets with .NextRecordset . This is not peculiar to ADO , but as far as I know applies to all client libraries, and is how SQL Server pass the information to the client. The only odd thing with ADO is that many programmers do not use .NextRecordset , or even know about it. I have also found that in some situations ADO may raise an error and say that .NextRecordset is not supported for your provider or cursor type.

The second gotcha is that your procedure may have more recordsets than you can imagine. To wit, INSERT, UPDATE and DELETE statements generate recordsets to report the rowcount, unless the setting NOCOUNT is ON .

Another irritating feature with ADO that I found, was that as soon there had been an error in the stored procedure, all subsequent result sets from the stored procedure were discarded. I could still tell from the return value of the stored procedure that execution had continued. I have found no combination where you can get the result sets that were produced after an error.

ADO also takes the freedom to make its own considerations about what is an error. I found that ADO always considers division by zero to be an error, even if both ARITHABORT and ANSI_WARNINGS are OFF . In this case, SQL Server merely produces a warning, but ADO opts to handle this warning as an error. A good thing in my opinion. Of what I have found, this only happens with division by zero; not with arithmetic errors such as overflow.

Above I said that even if I did not get all errors from SQL Server, ADO would raise an error. This is true as long as we are talking about commands you submit yourself. But ADO can submit commands behind your back, and if they result in errors, ADO may not alert you – even if the abort the batch and thereby rollback any outstanding transaction. This ugly situation is described further in KB article 810100.

Finally, a note on the return value and value of output parameters from a stored procedure. They are accessible from ADO , even if there is an error during execution of the stored procedure (as long the error does causes the procedure to terminate execution). If you use a client-side cursor you can normally access them directly after executing the procedure, whereas with a server-side cursor you must first retrieve all rows in all result sets. (Which means that if .NextRecordset is not supported for your cursor, you may not be able to retrieve the return value.) Beware that if you try to retrieve these values too soon, you will not be able to retrieve them even when you have retrieved all rows.

It is not really the topic for this text, but the reader might want to know my recommendation of what to choose from all these possibilities. And I say that you should use the SQLOLEDB provider (note that MSDASQL is the default), client-side cursors (note that server-side cursors is the default), invoke your stored procedures from the Command object, using adCmdStoredProcedure . Not because this is the best for error handling, but this appears to be the best from an overall programming perspective. (If you make these choices you will get a static read-only cursor.)

Note: this applies to ADO .Net 1.1. Since some behaviour I describe may be due to bugs or design flaws, earlier or later versions of ADO .Net may be different in some points.

To some extent, ADO .Net is much better fitted than ADO to handle errors and informational messages from SQL Server, but unfortunately neither ADO .Net is without shortcomings.

The ADO .Net classes can be divided into two groups. The disconnected classes that are common for all data sources, and the connected classes that are data-source specific, but.derived from a common interface. A group such of connected classes makes up a .Net Data Provider and each provider has its own name space. Three providers can connect to SQL Server: There is SqlClient, which is specific to SQL Server, and there are the OLE DB and ODBC .Net Data Providers that connect to anything for which there is an OLE DB provider or an ODBC driver. I will refer to them here as OleDb and Odbc, as this is how their namespaces are spelled in the .Net Framework.

If the only data source you target is SQL Server, SqlClient is of course the natural choice. As we shall see, however, there are situations where OleDb may be preferrable. There is even the odd case where Odbc is the best choice, but as I will detail later, you do best to avoid Odbc when connecting to SQL Server.

The three data providers have some common characteristics when it comes to handling of errors and messages from SQL Server, but there are also significant differences. I will first cover the common features.

To invoke a stored procedure from ADO .Net, you need a Command object. ( SqlCommand , OleDbCommand or OdbcCommand ). Normally you specify the CommandType as StoredProcedure and provide the procedure name as the command text, but you can also use the CommandType Text and specify an EXEC statement.

There are four methods that you can use to invoke a stored procedure from ADO .Net, and I list them here in the order you are most likely to use them:

DataAdapter.Fill Fills a DataTable or a DataSet with the data from the stored procedure. The are several overloaded Fill methods, some of which permit you to pass a CommandBehavior to specify that you want key or schema information, or that you want only a single row or a single result set.
ExecuteNonQuery Performs a command that does not return any result set (or if it does, you are not interested in it). One example is a store procedure that updates data.
ExecuteReader Returns a DataReader object, through which you can access the rows as they come from SQL Server. If there are several result sets, you use .NextResult to traverse them. This is the most general method to access data. Also here you can specify CommandBehavior.
ExecuteScalar Use this method to run a command that produces a result set of a single value.

To test the possible variations, I wrote a simple application in VB .Net, from which I could pass an SQL command or a stored procedure, and select which data provider and which call method to use. For most of the tests, I used a procedure that depending on input parameters would produce results sets, informational or error messages, possibly interleaved. What follows is based on my observations when playing with this application.

If an error occurs during execution of a stored procedure, the method you used to invoke the procedure will raise an exception. Thus, you should always call these methods within a Try-Catch block, so that you can handle the error message in some way. In the exception handler you have access to a provider-specific Exception object with an ErrorCollection , that containts information about the error. What information that is available is specific for the provider.

If you are interested in informational messages, that is messages with a severity ≤ 10, you can set up an InfoMessage event handler, which you register with the Connection object. It seems, though, if there are both errors and informational messages, that the informational messages comes with the exception. In the event handler, too, you have access to the ErrorsCollection from where you can retrieve the individual messages.

As long as you stick to Fill , ExecuteNonQuery and ExecuteScalar , your life is very simple, as all data has been retrieved once you come back, and if there is an error you wind up in your exception handler. Thus, in difference to ADO , you don’t have to bother about unexpected result sets and all that. If you want the return value of a stored procedure or the value of output parameters, these are available in the Parameters collection. However, the OleDb and Odbc providers normally do not fill in these values, if an error occurs during execution of a stored procedure.

If you use ExecuteReader , there are a few extra precautions. If the stored procedure first produces a result set, and then a message, you must first call .NextResult before you get an exception, or, for an informational message, any InfoMessage event handler is invoked. In difference to ADO, ADO .Net does not produce extra result sets for the rowcount of of INSERT, UPDATE and DELETE statements. However, under some circumstances, errors and messages may give cause to extraneous result sets.

Beware that if .NextResult throws an exception, it does not return a value, so if you have something like:

more_results retains the value it had before you called .NextResult . (Caveat: I’m not an experienced .Net programmer, but this is my observation.)

To get the return value from a stored procedure and the value of output parameters when you use ExecuteReader , you first have to retrieve all rows and all result sets for these values to be available.

Just like ADO, ADO .Net can sometimes generate commands behind your back; this appears mainly to happen when you use the CommandBehavior s KeyInfo and SchemaOnly . But in difference to ADO, ADO .Net communicates any SQL errors from these extra commands, and throws an exception in this case too.

So far, it may seem that ADO .Net is lot more well-behaving than ADO . To some extent it is, but I will now will procede to the specifics for each data provider, and this mainly deals with their respective shortcomings.

SqlClient

One very nice thing with SqlClient, is that the SqlError class includes all components of an SQL Server message: server, error number, message text, severity level, state, procedure and line number.

Another good thing with SqlClient, is that in difference to the other two providers, you do almost always get the return value and the value of output parameters from a stored procedure, even if there is an error during execution (provided that the error does not terminate the execution of the procedure, of course.).

But there are a couple of bad things too:

  • If the procedure produces more than one error, you only get one error message, unless you are using ExecuteNonQuery . This may be addressed by the fix described in KB 823679.
  • If the procedure produces an error before the first result set, you cannot access any data with any of the methods. ( ExecuteReader does not even return a SqlDataReader object.) If you need to access data in this case, Odbc is your sole possibility.
  • If the stored procedure produces a result set, then an error, then another result set, there is only one way to retrieve the second result set: use ExecuteReader and be sure to have SET NOCOUNT ON . If you run with NOCOUNT OFF , things can go really bad, and data may linger on the connection and come back when the connection is reused from the pool. Eventually SqlClient may get stuck in an infinite loop or throw some nonsensical exception.
  • RAISERROR WITH NOWAIT does not work with ExecuteNonQuery , but the messages are buffered as if there was no NOWAIT . Use any of the other methods, if you need RAISERROR WITH NOWAIT . (Note that to use NOWAIT ; you must use CommandType Text , and a single unparameterized SQL string, due to a bug in SQL Server.)

OleDb

In an OleDbErrorCollection , you don’t have access to all information about the error from SQL Server, but only the message text and the message number.

  • If there is an error message during execution, OleDb does in most situations not provide the return value of the stored procedure or the value of any output parameters.
  • If the procedure produces more than one error, you only get one error message if NOCOUNT is OFF . If NOCOUNT is ON , you may get all messages, unless there are result sets interleaved with the messages. For some reason the error messages comes in reverse order.
  • If the procedure produces an error before the first result set, you cannot access any data with any of the methods. ( ExecuteReader does not even return a OleDbDataReader object.) If you need to access data in this case, Odbc is your sole possibility.
  • If the stored procedure produces a result set, then an error, then another result set, there is only one way to retrieve the second and successive result sets: use ExecuteReader and be sure to have SET NOCOUNT OFF . If you have NOCOUNT ON , you will still get a lot of result sets, but most of them will be empty.
  • RAISERROR WITH NOWAIT does not always work with OleDb, but the messages are sometimes buffered. I have not been able to find a pattern for this. For NOWAIT to work at all, you must use CommandType Text , because a bug in SQL 2000,

In an OdbcErrorCollection , you don’t have access to all information about the error from SQL Server, but only the message text and the message number.

Odbc has all sorts of problems with errors and informational messages. If there are several informational messages, Odbc may lose control and fail to return data, including providing the return value and the values of output parameters of stored procedures. It does not matter whether you have declared an InfoMessage event handler. If there are error messages, and you try to retrieve data, you may get exceptions from the ODBC SQL Server driver saying Function sequence error or Associated statement not prepared .

If there are error messages before any result sets are produced, Odbc may not throw an exception for the first error message, but only invoke your InfoMessage event handler. And if you don’t have one, you will not even notice that there was an error. Under some circumstances more than one error message may be dropped this way.

Some of these problems may go away if you run with SET NOCOUNT ON , but not all. In general therefore, I’ll advice against using the Odbc .Net Data Provider to access SQL Server.

Still, there is one situation where Odbc is your sole choice, and that is if you call a stored procedure that first produces an error message and then a result set. The other two providers never return any data in this situation. With Odbc you can do it – but it is a narrow path to follow. You must have SET NOCOUNT ON . If you only have one result set, you can probably use OdbcDataAdapter.Fill . If there are more than one result set, you must use ExecuteReader , and you must specify the CommandBehavior SingleResult (!). You may get an exception about Function Sequence Error at the end, but by then you have retrieved all your data.

Thanks to Trevor Morris who pointed out the tidbit on IMPLICIT_TRANSACTIONS and error 266, Mark Williams and Paulo Santos who investigated DBCC OUTPUTBUFFER and SQL Server MVP Jacco Schalkwijk who found the definition of the severity levels 11-16.

If you have suggestions for improvements or corrections on contents, language or formatting, please mail me at esquel@sommarskog.se. If you have technical questions that any knowledgeable person could answer, I encourage you to post to any of the newsgroups microsoft.public.sqlserver.programming or comp.databases.ms-sqlserver.

2009-11-29 – Added a reference for the new, and still unfinished article for error handling in SQL 2005.

2008-03-31 – Thanks to Paulo Santos, there is now an improved stored procedure for retrieving the error message through DBCC OUTPUTBUFFER . Actually, the best way to get the scoop is to go to his web site directly.

2006-01-21 – Added a section on how to retrieve the text from an error message, and a description of severity levels 11-16.

2004-12-26 – Rewritten the section on TRY-CATCH in SQL 2005, to adapt for the Beta 2 changes.

2004-05-30 – One more case where error 266 is not raised.

2004-02-17 – Added a section on ADO .Net.

2003-12-03 – Rewrote section on the RETURN statement. Added section on TRY-CATCH in Yukon.

Источник

I am trying to execute a INSERT statement on a mySQL DB in C#:

MySqlConnection connection = new MySqlConnection("SERVER=" + _dbConnection + ";" +
   "DATABASE=" + _dbName + ";" +
   "PORT=" + _dbPort + ";" +
   "UID=" + _dbUsername + ";" +
   "PASSWORD=" + _dbPassword + ";");
MySqlDataAdapter adapter;
DataSet dataset = new DataSet();
command = new MySqlCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO plugins (pluginName, enabled) VALUES (@plugin,@enabled)";
command.Parameters.AddWithValue("@name", "pluginName");
command.Parameters.AddWithValue("@enabled", false);
adapter = new MySqlDataAdapter(command);
adapter.Fill(dataset);

The plugin table consists of two columns: pluginName(varchar(50)) and enabled(boolean).

This fails with the error: mysql Fatal error encountered during command execution. Any ideas on why this would fail?

asked Mar 18, 2010 at 4:52

Brian's user avatar

You seem to be using @plugin in the query but @name in the parameters, although I’d expect a better error message if that was the sole problem!

answered Mar 18, 2010 at 4:56

Chris's user avatar

ChrisChris

10.3k1 gold badge38 silver badges46 bronze badges

2

The error «Fatal error encountered during command execution» seems to be for general syntax errors in the CommandText line.

For example, the following just tripped me up for a while:

...@date,@email@first,@last...

Watch out for those commas! There should be one after @email:

...@date,@email,@first,@last...

answered Jul 12, 2012 at 21:03

DanM7's user avatar

DanM7DanM7

2,1853 gold badges27 silver badges46 bronze badges

Missing Parameters could also be a reason.
eg:

cmd.CommandText = "INSERT INTO login VALUES(@uname,@pwd);"
cmd.Parameters.AddWithValue("@uname",TxtUsername.Text.ToString())
'forgot to place "@pwd"

cmd.ExecuteNonQuery()

You get fatal error which can be solved by adding

cmd.Parameters.AddWithValue("@pwd",TxtPassword.Text.ToString())

answered Apr 15, 2019 at 6:38

Pranav Premarajan's user avatar

`(from user in _context.GetDataTable("call sp_get_user_subscribe_info(@param1, @param2)", _context.AddParameter(new dynamic[] { id }))`

My procedure has two parameters but I have passed only one parameter that’s why this error message occurred, But this error message is so confusing.

answered Jun 3, 2022 at 6:50

Md Somrat Akbor's user avatar

Some times you may observe 4014 error messages in SQL Server errorlog, like below
Error: 4014, Severity:20, State: 11.
A fatal error occurred while reading the input stream from the network. The session will be terminated

To continue analyze it, we should firstly know which is the issue client servers, and we can schedule to run below script during the issue most frequent time:

;WITH RingBufferConnectivity as
( SELECT
records.record.value(‘(/Record/@id)[1]’, ‘int’) AS [RecordID],
records.record.value(‘(/Record/ConnectivityTraceRecord/RecordType)[1]’, ‘varchar(max)’) AS [RecordType],
records.record.value(‘(/Record/ConnectivityTraceRecord/RecordTime)[1]’, ‘datetime’) AS [RecordTime],
records.record.value(‘(/Record/ConnectivityTraceRecord/SniConsumerError)[1]’, ‘int’) AS [Error],
records.record.value(‘(/Record/ConnectivityTraceRecord/State)[1]’, ‘int’) AS [State],
records.record.value(‘(/Record/ConnectivityTraceRecord/Spid)[1]’, ‘int’) AS [Spid],
records.record.value(‘(/Record/ConnectivityTraceRecord/RemoteHost)[1]’, ‘varchar(max)’) AS [RemoteHost],
records.record.value(‘(/Record/ConnectivityTraceRecord/RemotePort)[1]’, ‘varchar(max)’) AS [RemotePort],
records.record.value(‘(/Record/ConnectivityTraceRecord/LocalHost)[1]’, ‘varchar(max)’) AS [LocalHost]
FROM
( SELECT CAST(record as xml) AS record_data
FROM sys.dm_os_ring_buffers
WHERE ring_buffer_type= ‘RING_BUFFER_CONNECTIVITY’
) TabA
CROSS APPLY record_data.nodes(‘//Record’) AS records (record)
)
SELECT RBC.*, m.text
FROM RingBufferConnectivity RBC
LEFT JOIN sys.messages M ON
RBC.Error = M.message_id AND M.language_id = 1033
WHERE RBC.RecordType=’Error’ and RBC.Error=4014
ORDER BY RBC.RecordTime DESC

Check the column of ‘RemoteHost’ and find one of the most frequent IP, and you will need to collect network monitor between the server and SQL Server.

See below analysis sample of network trace:
Client sent 8000 bytes data:
123.45.0.789 789.123.0.45 TDS TDS:Continuous BulkLoadBCP, Version = Undefined TDS version(0x74000004), SPID = 0, PacketID = 4, Flags=…AP…, SrcPort=54541, DstPort=52343, PayloadLen=8000, Seq=1077286218 — 1077294218, Ack=3825055476, Win=262656

The packets was large so it was divided, but server only received 1460*5=7300 bytes of data, so it lost 700 bytes of data
123.45.0.789 789.123.0.45 TDS TDS:Continuous BulkLoadBCP, Version = Undefined TDS version(0x74000004), SPID = 0, PacketID = 4, Flags=…A…., SrcPort=54541, DstPort=52343, PayloadLen=1460, Seq=1077286218 — 1077287678, Ack=3825055476, Win=262656
123.45.0.789 789.123.0.45 TCP TCP:[Continuation to #96]Flags=…A…., SrcPort=54541, DstPort=52343, PayloadLen=1460, Seq=1077287678 — 1077289138, Ack=3825055476, Win=1026 (scale factor 0x8) = 262656
123.45.0.789 789.123.0.45 TCP TCP:[Continuation to #96]Flags=…A…., SrcPort=54541, DstPort=52343, PayloadLen=1460, Seq=1077289138 — 1077290598, Ack=3825055476, Win=1026 (scale factor 0x8) = 262656
123.45.0.789 789.123.0.45 TCP TCP:[Continuation to #96]Flags=…A…., SrcPort=54541, DstPort=52343, PayloadLen=1460, Seq=1077290598 — 1077292058, Ack=3825055476, Win=1026 (scale factor 0x8) = 262656
123.45.0.789 789.123.0.45 TCP TCP:[Continuation to #96]Flags=…A…., SrcPort=54541, DstPort=52343, PayloadLen=1460, Seq=1077292058 — 1077293518, Ack=3825055476, Win=1026 (scale factor 0x8) = 262656

Then the server reset the communication:
123.45.0.789 789.123.0.45 TCP TCP:Flags=…..R.., SrcPort=52343, DstPort=54541, PayloadLen=0, Seq=3825055476, Ack=1077293518, Win=0

So generally speaking, it was when the network tried to sperate a large packets, it lost one of them, it can be caused by TCPChimney offloading or other network communication issues, you need engage your network team to continue check where was the packet lost.

Ошибки и их обработка

PDO предлагает на выбор 3 стратегии обработки ошибок в зависимости от вашего стиля разработки приложений.

PDO::ERRMODE_SILENT

До PHP 8.0.0, это был режим по умолчанию. PDO просто предоставит вам код ошибки, который можно получить методами PDO::errorCode() и PDO::errorInfo() . Эти методы реализованы как в объектах запросов, так и в объектах баз данных. Если ошибка вызвана во время выполнения кода объекта запроса, нужно вызвать метод PDOStatement::errorCode() или PDOStatement::errorInfo() этого объекта. Если ошибка вызова объекта базы данных, нужно вызвать аналогичные методы у этого объекта.

PDO::ERRMODE_WARNING

Помимо установки кода ошибки PDO выдаст обычное E_WARNING сообщение. Это может быть полезно при отладке или тестировании, когда нужно видеть, что произошло, но не нужно прерывать работу приложения.

PDO::ERRMODE_EXCEPTION

Начиная с PHP 8.0.0 является режимом по умолчанию. Помимо задания кода ошибки PDO будет выбрасывать исключение PDOException , свойства которого будут отражать код ошибки и её описание. Этот режим также полезен при отладке, так как сразу известно, где в программе произошла ошибка. Это позволяет быстро локализовать и решить проблему. (Не забывайте, что если исключение является причиной завершения работы скрипта, все активные транзакции будут откачены.)

Режим исключений также полезен, так как даёт возможность структурировать обработку ошибок более тщательно, нежели с обычными предупреждениями PHP, а также с меньшей вложенностью кода, чем в случае работы в тихом режиме с явной проверкой возвращаемых значений при каждом обращении к базе данных.

Подробнее об исключениях в PHP смотрите в разделе Исключения.

PDO стандартизирован для работы со строковыми кодами ошибок SQL-92 SQLSTATE. Отдельные драйверы PDO могут задавать соответствия своих собственных кодов кодам SQLSTATE. Метод PDO::errorCode() возвращает одиночный код SQLSTATE. Если необходима специфичная информация об ошибке, PDO предлагает метод PDO::errorInfo() , который возвращает массив, содержащий код SQLSTATE, код ошибки драйвера, а также строку ошибки драйвера.

Пример #1 Создание PDO объекта и установка режима обработки ошибок

= ‘mysql:dbname=testdb;host=127.0.0.1’ ;
$user = ‘dbuser’ ;
$password = ‘dbpass’ ;

$dbh = new PDO ( $dsn , $user , $password );
$dbh -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );

// PDO выбросит исключение PDOException (если таблица не существует)
$dbh -> query ( «SELECT wrongcolumn FROM wrongtable» );
?>

Результат выполнения данного примера:

Метод PDO::__construct() будет всегда бросать исключение PDOException , если соединение оборвалось, независимо от установленного значения PDO::ATTR_ERRMODE .

Пример #2 Создание экземпляра класса PDO и установка режима обработки ошибок в конструкторе

= ‘mysql:dbname=test;host=127.0.0.1’ ;
$user = ‘googleguy’ ;
$password = ‘googleguy’ ;

$dbh = new PDO ( $dsn , $user , $password , array( PDO :: ATTR_ERRMODE => PDO :: ERRMODE_WARNING ));

// Следующий запрос приводит к ошибке уровня E_WARNING вместо исключения (когда таблица не существует)
$dbh -> query ( «SELECT wrongcolumn FROM wrongtable» );
?>

Источник

mysql_error

mysql_error — Возвращает текст ошибки последней операции с MySQL

Данный модуль устарел, начиная с версии PHP 5.5.0, и удалён в PHP 7.0.0. Используйте вместо него MySQLi или PDO_MySQL. Смотрите также инструкцию MySQL: выбор API. Альтернативы для данной функции:

Описание

Возвращает текст ошибки выполнения последней функции MySQL. Ошибки работы с MySQL больше не вызывают сообщений в PHP. Вместо этого используйте функцию mysql_error() , для получения сообщения об ошибке. Учтите, что функция возвращает текст ошибки только последней выполненной функции MySQL (исключая mysql_error() и mysql_errno() ), поэтому убедитесь, что вы вызываете данную функцию до вызова следующей функции MySQL.

Список параметров

Соединение MySQL. Если идентификатор соединения не был указан, используется последнее соединение, открытое mysql_connect() . Если такое соединение не было найдено, функция попытается создать таковое, как если бы mysql_connect() была вызвана без параметров. Если соединение не было найдено и не смогло быть создано, генерируется ошибка уровня E_WARNING .

Возвращаемые значения

Возвращает текст ошибки выполнения последней функции MySQL, или » (пустую строку), если операция выполнена успешно.

Примеры

Пример #1 Пример использования mysql_error()

= mysql_connect ( «localhost» , «mysql_user» , «mysql_password» );

mysql_select_db ( «nonexistentdb» , $link );
echo mysql_errno ( $link ) . «: » . mysql_error ( $link ). «n» ;

mysql_select_db ( «kossu» , $link );
mysql_query ( «SELECT * FROM nonexistenttable» , $link );
echo mysql_errno ( $link ) . «: » . mysql_error ( $link ) . «n» ;
?>

Результатом выполнения данного примера будет что-то подобное:

Смотрите также

  • mysql_errno() — Возвращает численный код ошибки выполнения последней операции с MySQL
  • » Коды ошибок MySQL

User Contributed Notes 14 notes

If you want to display errors like «Access denied. «, when mysql_error() returns «» and mysql_errno() returns 0, use $php_errormsg. This Warning will be stored there. You need to have track_errors set to true in your php.ini.

Note. There is a bug in either documentation about error_reporting() or in mysql_error() function cause manual for mysql_error(), says: «Errors coming back from the MySQL database backend no longer issue warnings.» Which is not true.

Be aware that if you are using multiple MySQL connections you MUST support the link identifier to the mysql_error() function. Otherwise your error message will be blank.

Just spent a good 30 minutes trying to figure out why i didn’t see my SQL errors.

Using a manipulation of josh ><>‘s function, I created the following. It’s purpose is to use the DB to store errors. It handles both original query, as well as the error log. Included Larry Ullman’s escape_data() as well since I use it in q().

function escape_data ( $data ) <
global $dbc ;
if( ini_get ( ‘magic_quotes_gpc’ )) <
$data = stripslashes ( $data );
>
return mysql_real_escape_string ( trim ( $data ), $dbc );
>

function q ( $page , $query ) <
// $page
$result = mysql_query ( $query );
if ( mysql_errno ()) <
$error = «MySQL error » . mysql_errno (). «: » . mysql_error (). «n
When executing:
n $query n
» ;
$log = mysql_query ( «INSERT INTO db_errors (error_page,error_text) VALUES (‘ $page ‘,’» . escape_data ( $error ). «‘)» );
>
>

// Run the query using q()
$query = «INSERT INTO names (first, last) VALUES (‘myfirst’, ‘mylast’» );
$result = q ( «Sample Page Title» , $query );
?>

When creating large applications it’s quite handy to create a custom function for handling queries. Just include this function in every script. And use db_query(in this example) instead of mysql_query.

This example prompts an error in debugmode (variable $b_debugmode ). An e-mail with the error will be sent to the site operator otherwise.

The script writes a log file in directory ( in this case /log ) as well.

The system is vulnerable when database/query information is prompted to visitors. So be sure to hide this information for visitors anytime.

$system_operator_mail = ‘developer@company.com’ ;
$system_from_mail = ‘info@mywebsite.com’ ;

function db_query ( $query ) <
global $b_debugmode ;

// Perform Query
$result = mysql_query ( $query );

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (! $result ) <
if( $b_debugmode ) <
$message = ‘Invalid query:
‘ . mysql_error () . ‘

raise_error ( ‘db_query_error: ‘ . $message );
>
return $result ;
>

function raise_error ( $message ) <
global $system_operator_mail , $system_from_mail ;

$serror =
«Env: » . $_SERVER [ ‘SERVER_NAME’ ] . «rn» .
«timestamp: » . Date ( ‘m/d/Y H:i:s’ ) . «rn» .
«script: » . $_SERVER [ ‘PHP_SELF’ ] . «rn» .
«error: » . $message . «rnrn» ;

// open a log file and write error
$fhandle = fopen ( ‘/logs/errors’ . date ( ‘Ymd’ ). ‘.txt’ , ‘a’ );
if( $fhandle ) <
fwrite ( $fhandle , $serror );
fclose (( $fhandle ));
>

// e-mail error to system operator
if(! $b_debugmode )
mail ( $system_operator_mail , ‘error: ‘ . $message , $serror , ‘From: ‘ . $system_from_mail );
>

My suggested implementation of mysql_error():

$result = mysql_query($query) or die(«A fatal MySQL error occured.n
Query: » . $query . «
nError: (» . mysql_errno() . «) » . mysql_error());

This will print out something like.

A fatal MySQL error occured.
Query: SELECT * FROM table
Error: (err_no) Bla bla bla, you did everything wrong

It’s very useful to see your query in order to detect problems with syntax. Most often, the output message from MySQL doesn’t let you see enough of the query in the error message to let you see where your query went bad- it a missing quote, comma, or ( or ) could have occured well before the error was detected. I do -not- recomend using this procedure, however, for queries which execute on your site that are not user-specific as it has the potential to leak sensative data. Recomended use is just for debugging/building a script, and for general user-specific queries which would at the worst, leak the users own information to themself.

When dealing with user input, make sure that you use
echo htmlspecialchars ( mysql_error ());
?>
instead of
echo mysql_error ();
?>

Otherwise it might be possible to crack into your system by submitting data that causes the SQL query to fail and that also contains javascript commands.

Would it make sense to change the examples in the documentation for mysql_query () and for mysql_error () accordingly?

some error can’t handle. Example:

ERROR 1044: Access denied for user: ‘ituser@mail.ramon.intranet’ to database ‘itcom’

This error ocurrs when a intent of a sql insert of no authorized user. The results: mysql_errno = 0 and the mysql_error = «» .

«Errors coming back from the MySQL database backend no longer issue warnings.» Please note, you have an error/bug here. In fact, MySQL 5.1 with PHP 5.2:

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host ‘locallllllhost’ (11001)

That’s a warning, which is not trapped by mysql_error()!

My suggested implementation of mysql_error():

$result = mysql_query($query) or die(«A fatal MySQL error occured.n
Query: » . $query . «
nError: (» . mysql_errno() . «) » . mysql_error());

This will print out something like.

A fatal MySQL error occured.
Query: SELECT * FROM table
Error: (err_no) Bla bla bla, you did everything wrong

It’s very useful to see your query in order to detect problems with syntax. Most often, the output message from MySQL doesn’t let you see enough of the query in the error message to let you see where your query went bad- it a missing quote, comma, or ( or ) could have occured well before the error was detected. I do -not- recomend using this procedure, however, for queries which execute on your site that are not user-specific as it has the potential to leak sensative data. Recomended use is just for debugging/building a script, and for general user-specific queries which would at the worst, leak the users own information to themself.

Oops, the code in my previous post only works for queries that don’t return data (INSERT, UPDATE, DELETE, etc.), this updated function should work for all types of queries (using $result = myquery($query);):

function myquery ($query) <
$result = mysql_query($query);
if (mysql_errno())
echo «MySQL error «.mysql_errno().»: «.mysql_error().»n
When executing:
n$queryn
«;
return $result;
>

This is a big one — As of MySQL 4.1 and above, apparently, the way passwords are hashed has changed. PHP 4.x is not compatible with this change, though PHP 5.0 is. I’m still using the 4.x series for various compatibility reasons, so when I set up MySQL 5.0.x on IIS 6.0 running PHP 4.4.4 I was surpised to get this error from mysql_error():

MYSQL: Client does not support authentication protocol requested by server; consider upgrading MySQL client

According to the MySQL site (http://dev.mysql.com/doc/refman/5.0/en/old-client.html) the best fix for this is to use the OLD_PASSWORD() function for your mysql DB user. You can reset it by issuing to MySQL:

Set PASSWORD for ‘user’@’host’ = OLD_PASSWORD(‘password’);

Источник

Fatal error: Uncaught exception ‘mysqli_sql_exception’ with message ‘No index used in query/prepared statement’

When I run the following code, I get the error saying

Fatal error: Uncaught exception ‘mysqli_sql_exception’ with message ‘No index used in query/prepared statement’

And this is the able schema —

5 Answers 5

The fatal error is not in MySQL; the missing index notification is a relatively low-severity warning.

The fatal error is in your PHP code, because of the following three conditions:

  • mysqli reports a lot of warnings, even for relatively harmless conditions.
  • You’re throwing mysqli_sql_exception for all errors and warnings due to your mysqli_report(MYSQLI_REPORT_ALL); line.
  • Your PHP code is not catching that exception (i.e. it’s not in a try<> block with an appropriate catch()<> block), and uncaught exceptions are fatal.

You can’t do much about the first one, as mentioned in the other answer. So, you can fix it either by changing your mysqli_report(. ) setting to MYSQLI_REPORT_STRICT or MYSQLI_REPORT_OFF , or indeed anything other than MYSQLI_REPORT_ALL .

(edit: w3d’s comment below gives a good explanation why, and suggests you could use mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) as a good alternative)

For best practices, and in combination with this, you should fix it properly by using try<> and catch()<> appropriately within your code.

Источник

Fatal error: Call to undefined function mysql_connect() [duplicate]

I have set up PHP, MySQL, and Apache. localhost() for PHP and it is working well. But after I downloaded MySQL, it reports:

Fatal error: Call to undefined function mysql_connect()

How can I fix this?

17 Answers 17

You upgraded to PHP 7, and now mysql_connect is deprecated. Check yours with:

Change it to mysqli_connect as in:

If you’re upgrading legacy PHP, now you’re faced with the task of upgrading all your mysql_* functions with mysqli_* functions.

If you get this error after upgrading to PHP 7.0, then you are using deprecated libraries.

mysql_connect — Open a connection to a MySQL Server
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

Open your terminal and run bellow command.

If you are running PHP you will also need to install the php module for mysql 5:

Verify that your installation of PHP has been compiled with mysql support. Create a test web page containing and load it in your browser. Search the page for MySQL. If you don’t see it, you need to recompile PHP with MySQL support, or reinstall a PHP package that has it built-in

Check if you forgot to enable the options below(loads the modules for mysql among others):

In PHP 7. You probably have PHP 7 in XAMPP. You now have two option: MySQLi and PDO.

For CPanel users, if you see this error and you already have PHP 5.x selected for the site, there might be a CPanel update that disabled mysql and mysqli PHP extensions.

To check and enable the extensions:

  1. Go to Select PHP Version in CPanel
  2. Make sure you have PHP 5.x selected
  3. Make sure mysql and mysqli PHP extensions are checked

This error is coming only for your PHP version v7.0. you can avoid these using PHP v5.0 else

i made only mysqli from mysql

A solution could be to use adapter functions like these to start using mysqli instead of mysql over your entire application:

I had this same problem on RHEL6. It turns out that the mysql.ini file in /etc/php.d only had a module name but needed a full path name. On my RHEL6 system the entry that works is:

After modifying the file, I restarted apache and everything worked.

My solution is here (I needed just to remove the last slash (NB: backward slashes) from PHPIniDir ‘c:PHP’ ): Fatal error: Call to undefined function mysql_connect() cannot solve

Am using windows 8 n this issue got resolved by changing the environment variables

follow these steps: Open my computer properties->advanced system settings->Environment variables. Under ‘system variables’, select ‘path’ and click on ‘edit’ In ‘variable value’, add ‘C:php;’ OR the path where php installed.

click OK and apply the settings and restart the system. It should work.

Here is a quick fix:

All the pros will probably hate me for this answer. But I got the same error on a server: Fatal error: Uncaught Error: Call to undefined function mysql_connect() that was using PHP 7. Did not have time to rewrite all the mysql code so a quick, temporary fix if anyone needs it is in CPANEL to look for PHP Configuration and change the version for that account to something like PHP 5.4 instead of PHP 7. Then the code worked fine without the above error.

If you are using Windows10, PHP 7.2 and try to connect to mysql. If this error occurred

Uncaught Error: Call to undefined function mysqli() in

The do the following steps to get it correct.

Источник

At 17:00 on 011 it was a principal.
At 17:16 on 012 it has been a Principal after The Auto Failover has been occurred.
At 17:17:59 on 012  the mirrored database has been recovered.
And during these 2 minutes on 012 from 17:16 until 17:17:59  a lot of processes have been terminated and a lot of dumps have been generated.
At 21:19  I have restarted the whole server 012  after that the mirroring has being worked successfully and 012 has been a Principal.
At 21:34  I executed a Manual Failover to make 011 a Principal.

Note: Before restarting the Whole server 012 I have restarted the SQL server services only on 011 but the Mirroring issue was still exist after that.

I got these error messages ON 011

*******************************************

Database mirroring connection error 4 ’64(failed to retrieve text for this error. Reason: 15105)’ for ‘TCP://server12.domain.local:5022’.

Database mirroring connection error 4 ‘An error occurred while receiving data: ‘121(failed to retrieve text for this error. Reason: 15105)’.’ for ‘TCP://witness.domain.local:5022’.

Database mirroring connection error 4 ‘An error occurred while receiving data: ’64(failed to retrieve text for this error. Reason: 15105)’.’ for ‘TCP://witness.domain.local:5022’.

Database mirroring connection error 2 ‘Connection attempt failed with error: ‘10060(failed to retrieve text for this error. Reason: 15105)’.’ for ‘TCP://server12.domain.local:5022’.

The client was unable to reuse a session with SPID 113, which had been reset for connection pooling. The failure ID is 29. This error may have been caused by an earlier operation failing. Check the error logs for failed operations immediately before this
error message.
********************************************
Process 105 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
Process 111 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
Process 133 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
Process 130 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION.
Process 82 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
Process 136 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
Process 111 generated fatal exception c0000005 EXCEPTION_ACCESS_VIOLATION
 

I will appreciate your feed back again

Environment:SERVER 2008 ENTERPRISE EDITION WITH SQL SERVER 2008 SP1 

  • Edited by

    Monday, December 19, 2011 10:15 AM

147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

1

11.05.2018, 16:20. Показов 4898. Ответов 13


Привет всем!

Моя программа работает с удаленной базой MySQL. Всё работает чудно, но после нескольких минут простоя программы (производится только обновление таблиц с сервера с интервалом в 5 секунд) при записи новой строки в таблицу MySQL выдает ошибку «Fatal error encountered during command execution«. Подскажите, пожалуйста, от чего это может случаться и как это исправить?

Спасибо!



0



Администратор

Эксперт .NET

15251 / 12289 / 4905

Регистрация: 17.03.2014

Сообщений: 24,888

Записей в блоге: 1

11.05.2018, 18:08

2

Vinemax, полный текст исключения смотрел? С соединением как работа идет — открывается каждый раз новое или все время одно?



2



Vinemax

147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

11.05.2018, 18:23

 [ТС]

3

OwenGlendower, всё время одно. Текст исключения весь не видел, бо у меня обработчик ошибок стоит. Я тут нагуглил, что есть такое свойство, как CommanTimeout, которое по умолчанию установлено в 30 (видимо, секунд). Устанавливаю его целенаправленно в 3600 и вот сейчас проверил и опять эта же ошибка после некоторого простоя. Код операции добавления строки в таблицу на VB у меня такой:

VB.NET
1
2
3
4
5
            Dim command As New MySqlCommand("INSERT INTO ...", connection)
 
            command.CommandTimeout = 3600
 
            command.ExecuteNonQuery()

…подключение connection всё время открыто, но вот command у меня локальная в процедуре. Может быть её сделать глобальной для всего класса и сразу установить ей глобально CommandTimeout ?



0



Администратор

Эксперт .NET

15251 / 12289 / 4905

Регистрация: 17.03.2014

Сообщений: 24,888

Записей в блоге: 1

11.05.2018, 18:27

4

Цитата
Сообщение от Vinemax
Посмотреть сообщение

Текст исключения весь не видел, бо у меня обработчик ошибок стоит.

Не вижу тут связи. Прежде чем что-то делать нужно точно знать что происходит. Получи полный текст исключения прежде чем двигаться дальше.

Цитата
Сообщение от Vinemax
Посмотреть сообщение

Может быть её сделать глобальной для всего класса и сразу установить ей глобально CommandTimeout ?

Не надо.

Цитата
Сообщение от Vinemax
Посмотреть сообщение

подключение connection всё время открыто

Возможно поможет открытие и закрытие соединение для каждой команды. Создали соединение, открыли, выполнили команду и закрыли соединение.



2



147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

11.05.2018, 18:43

 [ТС]

5

Цитата
Сообщение от OwenGlendower
Посмотреть сообщение

Создали соединение, открыли, выполнили команду и закрыли соединение.

Видимо, так и сделаю, если иначе не решу вопрос. Сейчас запустил без обработчика ошибок, подожду немного, чтобы увидеть весь текст исключения…

Добавлено через 12 минут
MySql.Data.MySqlClient.MySqlException: «Fatal error encountered during command execution.»

1) SocketException: Удаленный хост принудительно разорвал существующее подключение

2) IOException: Не удается прочитать данные из транспортного соединения: Удаленный хост принудительно разорвал существующее подключение.

3) MySqlException: Reading from the stream has failed.

4) MySqlException: Fatal error encountered attempting to read the resultset.

Это всё одна ошибка.



0



Администратор

Эксперт .NET

15251 / 12289 / 4905

Регистрация: 17.03.2014

Сообщений: 24,888

Записей в блоге: 1

11.05.2018, 18:53

6

Лучший ответ Сообщение было отмечено Yury Komar как решение

Решение

Vinemax, как и предполагалось виновато соединение которое внезапно оказывается закрытым. Попробуй открывать и закрывать его явно каждый раз.



2



147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

11.05.2018, 18:55

 [ТС]

7

Да вот не хотелось бы закрывать и открывать заново, это же всё время, особенно при медленном интернете



0



Администратор

Эксперт .NET

15251 / 12289 / 4905

Регистрация: 17.03.2014

Сообщений: 24,888

Записей в блоге: 1

11.05.2018, 18:56

8

Vinemax, у соединения две стороны. Одна сторона может закрыть соединение не уведомляя другую. В данном случае так поступает сервер MySql.



2



147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

11.05.2018, 19:30

 [ТС]

9

Лучший ответ Сообщение было отмечено Yury Komar как решение

Решение

OwenGlendower, сделал connection.Ping с интервалом в 5 секунд и проблема пропала. Только вот вопрос, а можно ли так часто и много пинговать, админов сайта это не насторожит, не заблокируют?



1



Администратор

Эксперт .NET

15251 / 12289 / 4905

Регистрация: 17.03.2014

Сообщений: 24,888

Записей в блоге: 1

11.05.2018, 20:05

10

Vinemax, без понятия. Время покажет.



1



147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

11.05.2018, 20:24

 [ТС]

11

OwenGlendower, спасибо!



0



Модератор

Эксперт .NET

3861 / 3184 / 479

Регистрация: 27.01.2014

Сообщений: 5,809

12.05.2018, 04:21

12

Vinemax, а зачем каждые 5 секунд делать пинг, ведь это гораздо дольше чем открывать и закрывать соединение?



1



147 / 115 / 10

Регистрация: 12.09.2011

Сообщений: 785

13.05.2018, 02:15

 [ТС]

13

Yury Komar, ну у меня происходит так… В фоновом процессе BackgroundWorker’а каждые пять секунд обновляются все таблицы программы, а точнее измененные строки всех таблиц. При запуске BackgroundWorker’а пятисекундный таймер отключается, а при завершении обновления всех таблиц опять включается, то есть через пять секунд из процедуры Tick таймера BackgroundWorker опять запускается. Ну вот в этот момент перед запуском BackgroundWorker’а я и делаю пинг. И вот практика на примере очень медленного Интернета показала, что пинг происходит очень быстро, а вот новое подключение идет две — три секунды. Дело в том, что MySqlConnection.Ping возвращает только True или False, то есть не дает всей инфы, отчего, видимо, быстро и срабатывает. Здесь получается суть не конкретно в пинге, а в том, чтобы connection хоть чем-то занять, дабы оно не разъединялось.



1



Модератор

Эксперт .NET

3861 / 3184 / 479

Регистрация: 27.01.2014

Сообщений: 5,809

13.05.2018, 02:42

14

Vinemax, понятно. Ну надеюсь будет работать без сюрпризов.



1



Понравилась статья? Поделить с друзьями:
  • Insmod normal error no such partition
  • Insmod normal error file not found
  • Insmod error could not insert module unknown symbol in module
  • Insmod error could not insert module ko invalid module format
  • Insite ошибка 5204