久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在循環(huán) SQL 查詢中捕獲多個錯誤

Catching multiple errors in loop SQL query(在循環(huán) SQL 查詢中捕獲多個錯誤)
本文介紹了在循環(huán) SQL 查詢中捕獲多個錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有下面的插入查詢,它從 OriginalData 表中選擇記錄,其中一切都是數(shù)據(jù)類型 nvarchar(max) 并將其插入到具有特定列的臨時表中定義,即 MainAccountINT 類型.

I have the below insert query which selects records from the OriginalData table where everything is of datatype nvarchar(max) and inserts it into the temp table which has specific column definitions i.e MainAccount is of type INT.

我正在逐行插入,因為如果 OriginalData 表中有一條記錄,其中 MainAccount 值為Test",它顯然會導致轉換錯誤并且插入將失敗.begin try 塊用于更新包含錯誤的表.

I am doing a row by row insert because if there is a record in OriginalData table where the MainAccount value is 'Test' the it will obviously cause a conversion error and the insert will fail. The begin try block is used to update the table with the error.

但是,如果同一行有多個錯誤,我希望能夠同時捕獲它們,而不僅僅是第一個.

However if there are multiple errors on the same row I want to be able to capture them both and not just the first one.

TRUNCATE TABLE [Temp] 

DECLARE @RowId INT, @MaxRowId INT  

SET @RowId = 1

SELECT @MaxRowId = MAX(RowId) 
FROM [Staging].[FactFinancialsCoded_Abbas_InitialValidationTest]

WHILE(@RowId <= @MaxRowId)
BEGIN
BEGIN TRY  
    INSERT INTO [Temp] (ExtractSource, MainAccount, 
                        RecordLevel1Code, RecordLevel2Code, RecordTypeNo, 
                        TransDate, Amount, PeriodCode, CompanyCode)
        SELECT 
            ExtractSource, MainAccount,
            RecordLevel1Code, RecordLevel2Code, RecordTypeNo,
            TransDate, Amount, PeriodCode, DataAreaId
        FROM 
            [Staging].[FactFinancialsCoded_Abbas_InitialValidationTest]  
        WHERE 
            RowId = @RowId;

    PRINT @RowId;

END TRY  
BEGIN CATCH  

Update [Staging].[FactFinancialsCoded_Abbas_InitialValidationTest]  
Set ValidationErrors = ERROR_MESSAGE()
where RowId = @RowId  

END CATCH 
SET @RowId += 1;      
END

推薦答案

我沒有這樣做,而是通過在要轉換為非字符串列的每一列上使用 TRY_PARSE() 或 TRY_CONVERT() 來處理此問題.

Instead of doing it this way, I handle this by using TRY_PARSE() or TRY_CONVERT() on each column that I am converting to a non-string column.

如果您隨后需要將驗證失敗存儲在另一個表中,您可以再次獲取源表中具有非空值且目標表中具有空值的所有行,然后插入這些行進入您的驗證失敗"表.

If you then need to store the validation failures in another table, you can make a second pass getting all the rows that have a non-null value in the source table and a null value in the destination table, and insert those rows into your "failed validation" table.

這篇關于在循環(huán) SQL 查詢中捕獲多個錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 欧美久久一级特黄毛片 | 精品久久久久久亚洲精品 | 午夜视频一区二区三区 | 羞羞视频网页 | 九九九视频在线观看 | 欧美专区在线 | 国产精品久久久久久久久久 | 国产精品久久二区 | 喷潮网站 | 国产欧美一区二区久久性色99 | 日韩精品在线看 | 精品自拍视频 | 成人天堂噜噜噜 | 精品欧美视频 | 成人午夜电影在线观看 | 97超碰在线免费 | www.国产精 | 国产不卡在线播放 | 羞羞涩涩在线观看 | 亚洲高清久久 | 91成人在线视频 | 免费一区二区在线观看 | 欧美不卡视频 | 中文日韩在线 | 久久久国产一区二区三区 | 影视一区| 福利视频网址 | 亚洲国产精品自拍 | 欧美日韩综合 | 91看片在线观看 | 亚洲国产精品一区二区第一页 | 欧美综合国产精品久久丁香 | 婷婷综合五月天 | 成人欧美日韩一区二区三区 | www亚洲一区| 午夜免费 | 色综合色综合网色综合 | 午夜男人视频 | 欧美亚洲激情 | 在线高清免费观看视频 | 2019天天干夜夜操 |