Get Specific Info From SQL Error Message 547

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

How do I correctly extract specific info from an sql error message number 547?

Info Required:

  • Table Name
  • Constraint Name
  • Column Name

Code:

Try    .... Catch ex As System.Data.SqlClient.SqlException          If ex.Number = 547 Then          End If End Try 

Sample message:

UPDATE statement conflicted with COLUMN CHECK constraint 'CK_Birthdate'. The conflict occurred in database 'Northwind', table 'Employees', column 'BirthDate'.

回答1:

There is no straight forward way of getting these pieces of information separately.

It all gets concatenated into the error message.

You can use select * from sys.messages where message_id=547 to see the various different language formats of the message that you would need to deal with in order to extract the constituent parts then perhaps use regular expressions with capturing groups based around this information.



回答2:

In addition to queries, here's a powershell script which wraps the sys.messages queries.

http://blogs.msdn.com/b/buckwoody/archive/2009/04/30/and-the-winner-is-get-sql-server-error-messages-from-powershell.aspx



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!