2014-01-13 64 views
1

我有一個場景,我從一個.TXT文件源生成兩個.CSV文件。使用條件拆分多個空字符串

一個所要求的轉換是這樣的:

  • 確定包含與空字符串或NULL值
  • 至少一個字段記錄發送這些記錄到一個單獨的.CSV文件目的地

我需要檢查跨多個字段的這種情況。

例如,我有這些領域在我的源.TXT文件:

col1 
col2 
col3 
col4 
col5 
col6 
col7 

我需要檢查,看看是否有這些字段包含空字符串或NULL值。

我使用這個公式有條件拆分轉換:

col1 !="" 

,當我嘗試寫的所有列的公式在我的文本文件按預期工作的一列,但並不如預期:

col1 !="" || col2 !=""... 

其中一個字段中有空或NULL值的某些行結束於錯誤的.CSV文件中。

任何幫助表示讚賞!

+1

空和空是不一樣的東西。你的意思是什麼不能評估病情?你收到錯誤信息嗎? – Jayvee

+0

不,它可以與文件中的所有行成功運行,但不會評估條件並將記錄拆分到其他文件中。 – ChelChabiloGujrati

+1

它可能是cols不是空的,但有空格;嘗試修剪列,例如TRIM(col1)!=「」|| TRIM(col1)!=「」|| ... – Jayvee

回答

3

如果我理解正確你的問題,我想你只需要使用下面的公式在有條件拆分轉換:

ISNULL(col1) || ISNULL(col2) || ISNULL(col3) || ISNULL(col4) || ISNULL(col5) || ISNULL(col6) || ISNULL(col7) || TRIM(col1) == "" || TRIM(col2) == "" || TRIM(col3) == "" || TRIM(col4) == "" || TRIM(col5) == "" || TRIM(col6) == "" || TRIM(col7) == "" 

有一個空字符串和NULL之間的差異,但因爲你的源代碼是.TXT文件,這可能不是那麼明顯,因爲如果您的源代碼是數據庫表格。

Here is the SQL Fiddle for the sample data

條件性拆分轉換編輯器中的數據流的

Screenshot of the Conditional Split Transformation Editor

截圖的截圖結果

Screenshot of the Data Flow results

希望這有助於。

+0

謝謝你的回覆。但因爲我的來源是平面文件(.txt)。 – ChelChabiloGujrati

+0

不客氣。概念和公式相同:使用「平面文件源」而不是「OLE DB源」。我使用「OLE DB Source」來突出顯示空字符串和NULL之間的區別。 –

+0

我盡了一切可能的方式,即使是你提到但沒有成功的方法。我認爲我必須要採用暫存表的方法。 – ChelChabiloGujrati

0

我想通了。在條件分割之前使用腳本組件來標記行。

代碼:

/* Microsoft SQL Server Integration Services Script Component 
* Write scripts using Microsoft Visual C# 2008. 
* ScriptMain is the entry point class of the script.*/ 

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Pipeline.Wrapper; 
using Microsoft.SqlServer.Dts.Runtime.Wrapper; 
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] 
public class ScriptMain : UserComponent 
{ 
/**string RecordID = ""; 
string ClientRequestID = ""; 
string AccountID = ""; 
string ClientMemberID = ""; 
string MemberDOB = ""; 
string ProviderPhone = "";**/ 
string Flag = "True"; 

public override void Input0_ProcessInputRow(Input0Buffer Row) 
{ 
    if (Row.ClientRequestID == "" || Row.ClientMemberID == "" || Row.AccountID == "" || Row.MemberDOB == "" || Row.ProviderPhone == "") 
    { 

     Flag = "False"; 
    } 
    else 
    { 
     Flag = "True"; 
    } 
    Row.Flag = Flag; 
}   

}

enter image description here

enter image description here

+0

仔細查看腳本中的表情以及原始示例中的表情。你看得到差別嗎?一個使用'!=',另一個使用'=='。這些是不同的邏輯表達式。如果你的邏輯表達式正確,你的原始方法可能會奏效。 –

+0

@ElectricLlama - 你是對的。但我將這些行標記爲false,並在條件分割中僅傳遞true,正如您所看到的那樣。這解決了我的問題。 – ChelChabiloGujrati

+0

它們是完全不同的布爾表達式。 'Col1!=「」|| Col2!=「」'與Col1 =「」||完全不同Col2 =「」'。如果你翻轉結果你會得到不同的答案 - 試試看看。 –