开发者

When does SQL SELECT statements throw exceptions?

TSQL here. Specifically Server 2008(literally just upgraded)

Concerning stored procedures: Try/Catch

I was trying to make a list of cases when a Select Statement will throw an exception. The ones I can think of are syntax related(includes null variables) a开发者_如何学Cnd divide by zero. I'm only guessing there are just a whole boat load of them for Insert/Alter and Create/Truncate.

If you happen to know of a good source link, that would be great.

This question came up when I was reading this exhaustive blog post about error handling for SQL server. It's titled for SQL Server 2000, but I think most of it still applies.

edit

Sorry, I meant to link this earlier. . .

http://msdn.microsoft.com/en-us/library/aa175920(v=sql.80).aspx


Outside for compile ("didnt' run") errors, you have at least these runtime errors

  • arithmetic errors
    These change based on various SET statement
    Example: get sql server to warn about truncation / rounding

  • overflow errors
    example: one of the rows overflows smallint in some calculation

  • CAST errors
    eg you try ISNUMERIC in a WHERE or CASE and try to cast 'bob^' or 1.23 to int
    See Why use Select Top 100 Percent?

However, you'd always want to use TRY/CATCH though, surely...?


Adding to gbn's post, you can also get locking errors like lock wait timeouts and deadlocks.

If you are referencing #Temp tables, you can get "Invalid object name '#Temp'" errors, because these are unbound until the statement executes.

If you are in READ UNCOMMITTED or WITH (NOLOCK), you can get error: 601 - "Could not continue scan with NOLOCK due to data movement."

If your code runs .NET code, you would probably get exceptions from there.

If your code selects from a remote server, you could a whole different set of errors about connections.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜