你为什么不换行抛出异常到try/catch块的SQL有更多的信息关于它
BEGIN TRY
SELECT *
FROM sys.messages
WHERE message_id = 21;
END TRY
GO
-- The previous GO breaks the script into two batches,
-- generating syntax errors. The script runs if this GO
-- is removed.
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber;
END CATCH;
GO
下都是错误的信息,您可以检查
ERROR_NUMBER() returns the error number.
ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters such as lengths, object names, or times.
ERROR_SEVERITY() returns the error severity.
ERROR_STATE() returns the error state number.
ERROR_LINE() returns the line number inside the routine that caused the error.
ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
http://msdn.microsoft.com/en-us/library/ms179296.aspx
如果这还不够,你甚至可以编写一个查询来选择所有记录,其中某些领域是无法转换为数字(在你的情况是有NVarChar不可兑换)
以下示例使用ISNUMERIC返回非数值的所有邮政编码。
SELECT City, PostalCode
FROM Person.Address
WHERE ISNUMERIC(PostalCode)<> 1
http://msdn.microsoft.com/en-us/library/ms186272.aspx
是否要提取数字部分(如果存在)或忽略包含字符数据的所有行? – niktrs
不,我只是想找到导致错误的值。在上面的例子'a'。 –