SQL Server: Find out what row caused the TSQL to fail (SSIS)

前端 未结 7 1255
醉话见心
醉话见心 2021-01-13 01:57

SQL Server 2005 Question:

I\'m working on a data conversion project where I\'m taking 80k+ rows and moving them from one table to another. When I run the TSQL, it bo

7条回答
  •  粉色の甜心
    2021-01-13 02:12

    Not exactly a cursor, but as effective - I had over 4 million rows to examine with multiple conversion failrues. Here is what I used, and it resulted in a two temp tables one with all my values and assigned rows and one that simply contained a list of rows in the first temp table that failed to convert.

    select row_number() over (order by TimeID) as rownum,timeID into #TestingTable from MyTableWithBadData
    
    set nocount on
    declare @row as int
    declare @last as int
    set @row=0
    select @last = count(*) from #TestingTable
    declare @timeid as decimal(24,0)
    create table #fails (rownum int)
    while @row<=@last
    begin
        Begin Try
            select @timeid=cast(timeID as decimal(24,0)) from #TestingTable where rownum = @row 
        end try
        begin catch 
            print cast(@row as varchar(25)) + ' : failed'
            insert into #fails(rownum) values(@row)
        end catch
        set @row = @row+1
    end
    

提交回复
热议问题