How to interpret SQL Server errors? - sql-server

Whenever I make a syntax error in a SQL Server query, I get an error message which I find pretty hard to interpret. For example the error that I have right now is
Msg 102, Level 15, State 1, Procedure myQuery, Line 3 [Batch Start Line 57]
Incorrect syntax near '!'.
I used table!column instead of table.column. I fixed my problem because I understood this bit:
Incorrect syntax near '!'
However, I do not understand how to interpret this message
Msg 102, Level 15, State 1, Procedure myQuery, Line 3 [Batch Start Line 57]
Do people usually just ignore that part? I have been getting along just fine without paying much attention to it. But being able to understand the error message might help be locate exactly where the error is.
Edit:
I don't understand what Msg, Level, State, Batch and Start Line means
Edit2:
Besides the accepted answer this link also helped me clear up my misunderstanding of what line number means. I always thought that line number meant the line in the SQL query where the error is which confused me. Turns out the line number is the line in the query window. To enable line number in query editor window go to :
Tools > Options > Text Editor > Transact-SQL > General >Line Numbers
Press OK

From Understanding Database Engine Errors:
Errors raised by the Microsoft SQL Server Database Engine have the (following) attributes:
Error number
Each error message has a unique error number.
Error message string
The error message contains diagnostic information about the cause of the error. Many
error messages have substitution variables in which information, such as the name of
the object generating the error, is inserted.
Severity
The severity indicates how serious the error is. Errors that have a low severity, such
as 1 or 2, are information messages or low-level warnings. Errors that have a high
severity indicate problems that should be addressed as soon as possible.
State
Some error messages can be raised at multiple points in the code for the Database
Engine. For example, an 1105 error can be raised for several different conditions.
Each specific condition that raises an error assigns a unique state code.
When you are viewing databases that contain information about known issues, such as
the Microsoft Knowledge Base, you can use the state number to determine whether the
recorded issue is the same as the error you have encountered. [...]
Procedure name
Is the name of the stored procedure or trigger in which the error has occurred.
Line number
Indicates which statement in a batch, stored procedure, trigger, or function generated
the error.
Specifically, [Batch Start Line 57] means that you are running a script with multiple batches - (if it's in SSMS the batches are separated by the GO keyword) - so the error is in the 3rd line of the batch that starts in line 57 of the script.
Here's a break down of the error message you've posted to it's parts:
ErrorNumber
Msg 102,
Severity
Level 15,
State
State 1,
Procedure
Procedure myQuery,
Line number
Line 3 [Batch Start Line 57]
Error message string
Incorrect syntax near '!'.

Unfortunately, this did not work for me. I use the latest version of SSMS or sql server management studio. I have a SQL script (in query editor) which has about 100 lines of code. This is error I got in the query:
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the nvarchar value 'abcd' to data type int.
The error message is either incorrect or simply misleading. The actual error on line number 70 in the query editor ! When, I click on the error message, it takes me to line 2 which is just a simple select top 1. Google searches revealed that the error had something to do with String concatenation, but there was no concatenation in my sql code.
Luckily, I remembered that I had seen this kind of error before when I forgot to enclose a number (in varchar column) in single quotes. So, it looks like SSMS is useless when it comes to debugging errors.

Related

SQL Server drops previous error in Try-Catch block (Also in 2019)

I have a problem with error handling. I'm using Try/Catch-blocks and this works very good for me. But in some cases there's not only one error raised but more. Then I'm only getting the last error message.
Let me give an example :
When executing this block :
alter table [dbo].[table]
add constraint [somefk]
foreign key ([somecol]) references [dbo].[parenttab] ([someid])
it raises 2 errors:
Msg 1778, Level 16, State 0, Line 7
Column 'dbo.parenttab.someid' is not the same data type as referencing column 'table.somecol' in foreign key 'somefk'.
Msg 1750, Level 16, State 1, Line 7
Could not create constraint or index. See previous errors.
So far, so good. But in case I put the statement in a Try/Catch block, the first message (1778) "is eaten" by someone.
In the end, I'm getting only the second message with the non-significant text :(
So, what I'm missing is a kind of an error collection, well-known from programming languages or frameworks. Or is it just a "Missing feature" in SQL Server...? How can I get the proceeding error message?
Thanks a lot in advance and best regards!
fksof

Prefix Error for Table and Server Call

I'm getting the following error(s)
Msg 117, Level 15, State 1, Procedure lp_..._data, Line 153
The object name 'abcDBProd.Intermediate.dbo.upld_data' contains more than the maximum number of prefixes. The maximum is 2.
Msg 117, Level 15, State 1, Procedure lp_..._Tables, Line 520
The object name 'ABCDBPROD.Intermediate.dbo.UPLD_data' contains more than the maximum number of prefixes. The maximum is 2.
If I understand correctly the purpose/meaning of the error that I can't have more then 2 prefixes prior to the table name. In the first statement i'm using the name of the Server itself and in the second case the LinkedServer name. The confusion I've got is that i have many tables on this server/databse and they are all connected the same and none of them are causing this error. but this one is.
What do I do? Is it a permission issue or security issue? I'm not sure where to look.
Does it matter if the command calling the table, truncates, inserts or updates the data? Are there restrictions like I can't truncate but I can insert? Why does it work in some cases but not in others?
Linked tables are not allowed in some contexts, such as DROP TABLE and INSERT INTO. If you use a name that includes the server name, then you will get this error.
For instance, this is not allowed:
drop table abcDBProd.Intermediate.dbo.upld_data;
You may be able to work around this with openquery().

Does ADODB.Stream have a maximum file size it can handle?

I'm using Microsoft SQL Server 2005, and exporting image data types to files (From a Sharepoint database), using a script not too dissimilar to the one found here
It works well, but for some reason with it fails on files that are more than ~45MB:
Msg 0, Level 11, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
So, I've had to add the argument WHERE DATALENGTH(Content)/1024 <= 45000 so that it runs through the files it can without failing.
Does anyone know if ADODB.Stream has a maximum file size it can handle?
I'm not much into Sharepoint's nor SQL Server processes and stuff, however, I was looking for something like this regarding Classic ASPs (that's how I reached your question). In my case it seems like the Response.BinaryWrite method has a 20MB limitation and I had send the stream in several smaller parts.
Maybe there's the same thing here.
Check this post and it's answers, I think it may have some clues for what you're looking.
Good luck!

What exactly does the T-SQL "LineNo" reserved word do?

I was writing a query against a table today on a SQL Server 2000 box, and while writing the query in Query Analyzer, to my surprise I noticed the word LineNo was converted to blue text.
It appears to be a reserved word according to MSDN documentation, but I can find no information on it, just speculation that it might be a legacy reserved word that doesn't do anything.
I have no problem escaping the field name, but I'm curious -- does anyone know what "LineNo" in T-SQL is actually used for?
OK, this is completely undocumented, and I had to figure it out via trial and error, but it sets the line number for error reporting. For example:
LINENO 25
SELECT * FROM NON_EXISTENT_TABLE
The above will give you an error message, indicating an error at line 27 (instead of 3, if you convert the LINENO line to a single line comment (e.g., by prefixing it with two hyphens) ):
Msg 208, Level 16, State 1, Line 27
Invalid object name 'NON_EXISTENT_TABLE'.
This is related to similar mechanisms in programming languages, such as the #line preprocessor directives in Visual C++ and Visual C# (which are documented, by the way).
How is this useful, you may ask? Well, one use of this it to help SQL code generators that generate code from some higher level (than SQL) language and/or perform macro expansion, tie generated code lines to user code lines.
P.S., It is not a good idea to rely on undocumented features, especially when dealing with a database.
Update: This explanation is still correct up to and including the current version of SQL Server, which at the time of this writing is SQL Server 2008 R2 Cumulative Update 5 (10.50.1753.0) .
Depending on where you use it, you can always use [LineNo]. For example:
select LnNo [LineNo] from OrderLines.

How to get around UDF restriction on side-effecting operators?

Microsoft Books Online (BOL) on Using Change Data explains a misleading error messages for cdc.fn_cdc_get_all_changes_* & cdc.fn_cdc_get_net_changes_* when an invalid, out-of-range LSN (Log Sequence Number) has been passed to them.
Msg 313, Level 16, State 3, Line 1
An insufficient number of arguments were supplied for the procedure or function `cdc.fn_cdc_get_all_changes_` ...
Msg 313, Level 16, State 3, Line 1
An insufficient number of arguments were supplied for the procedure or function `cdc.fn_cdc_get_net_changes_` ...
Their explanation on this misleading error message is as following
Note:
It is recognized that the
message for Msg 313 is misleading and
does not convey the actual cause of
the failure. This awkward usage stems
from the inability to raise an
explicit error from within a TVF.
Nevertheless, the value of returning a
recognizable, if inaccurate, error was
deemed preferable to simply returning
an empty result. An empty result set
would not be distinguishable from a
valid query returning no changes.
Here is a demonstration of what the note meant RAISERROR
There are times when I'd like to throw an error and you cannot use TRY..CATCH within UDF since it also has a side-effect like RAISERROR.
Now the question is, how do you get around this problem?
I am sure that you have faced with this restriction before.
What alternative would you suggest?
[UPDATE] Let's suppose that you are forced to use UDF.
In the case of your function which returns BIT, I'll return NULL back to the calling code, and at some point after using the function check for NULL, and throw an error then.

Resources