Sql server 2008 r2 Sql Server是否使用Try Catch从行本身获取任何数据?
在SQLServer2008R2中使用Try/Catch,是否有技巧从导致错误的行中获取一些信息?比如说,Sql server 2008 r2 Sql Server是否使用Try Catch从行本身获取任何数据?,sql-server-2008-r2,Sql Server 2008 R2,在SQLServer2008R2中使用Try/Catch,是否有技巧从导致错误的行中获取一些信息?比如说, BEGIN TRY INSERT INTO MyTable SELECT * FROM @MyTableVar END TRY BEGIN CATCH -- In here, is there some way to know, for example, MyTable.SomeColumn for the offending ro
BEGIN TRY
INSERT INTO MyTable
SELECT *
FROM @MyTableVar
END TRY
BEGIN CATCH
-- In here, is there some way to know, for example, MyTable.SomeColumn for the offending row?
END CATCH
这就是我最后做的:
DECLARE @MyResults TABLE (
Id INT IDENTITY( 1, 1 )
TheKey VARCHAR(20),
Success BIT
)
-- Initially set Success to 1 for all rows
INSERT INTO @MyResults
SELECT TheKey, 1
FROM @MyTableVar
DECLARE @CurrentKey VARCHAR(20)
DECLARE @CurrentId INT
DECLARE incoming CURSOR FOR SELECT Id, TheKey FROM @MyResults
OPEN incoming
FETCH incoming into @Id, @CurrentKey
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
INSERT INTO OfficialTable
SELECT *
FROM @MyTableVar TV
WHERE TV.TheKey = @CurrentKey
END TRY
BEGIN CATCH
-- On any failure, update the proper row in @MyResults
UPDATE @MyResults
SET Success = 0
WHERE TheKey = @CurrentKey AND Id = @CurrentId
END CATCH
FETCH NEXT FROM incoming INTO @Id, @CurrentKey
END
CLOSE incoming
SELECT * FROM @MyResults
在这里,我知道@MyTableVar的关键,因此,我应该能够用它查找我需要的任何东西