如何让SQL Server 2008r2向我显示错误?

时间:2010-12-03 16:47:23

标签: sql sql-server sql-server-2008 sql-server-2008-r2

我正在运行一个Insert脚本,它应该将13,381行插入SSMS的空白数据库中。它告诉我“查询已完成但有错误”并且只插入了13357行。

错误列表中没有显示任何内容。如何在脚本中找到错误?

谢谢!

1 个答案:

答案 0 :(得分:35)

试试这个:

begin try

    --your inserts here

end try
begin catch
    --returns the complete original error message as a result set
    SELECT 
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage

    --will return the complete original error message as an error message
    DECLARE @ErrorMessage nvarchar(400), @ErrorNumber int, @ErrorSeverity int, @ErrorState int, @ErrorLine int
    SELECT @ErrorMessage = N'Error %d, Line %d, Message: '+ERROR_MESSAGE(),@ErrorNumber = ERROR_NUMBER(),@ErrorSeverity = ERROR_SEVERITY(),@ErrorState = ERROR_STATE(),@ErrorLine = ERROR_LINE()
    RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState, @ErrorNumber,@ErrorLine)
end catch