从CSV文件获取列名称并使用控制文件作为数据加载到DB中

时间:2017-09-18 18:41:31

标签: csv batch-file sql-loader

我的控制文件是这样的:

OPTIONS (skip=1) 
LOAD DATA

APPEND

INTO TABLE Temp_Wide

FIELDS TERMINATED BY ","

TRAILING NULLCOLS

(

nothing        boundfiller,

Date_Recorded "TO_DATE(:nothing || :Date_Recorded , 'mm/dd/yyyy hh24:mi:ss')",

Millitm CHAR,

nothing1        boundfiller,

TEMPERATURE CHAR,

nothing2        boundfiller,

Tagname (Name of Column4 in input CSV)

)

在我的批处理脚本中,我正在调用控制文件和输入CSV文件。

我需要从该特定输入CSV获取插入标记名数据作为column4的名称,但我不确定如何执行此操作。

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容获取列名称:

people: 
 -KuM2GgA5JdH0Inem6lGaddclose
   appliedTo: "-KuM1IB5TisBtc34y2Bb"
   document: "docs/837c2500-9cbe-11e7-8ac1-17a6c37e2057"
   name: "Test Testerson"

private string getColName(string pathToCSV, int colIndex1Based)
{
    string retval = string.Empty;//getting a variable ready to host the return value
    using (StreamReader sr = new StreamReader(pathToCSV))//read the data file
    {
        string currentLine = string.Empty;//just initializing a var
        if ((currentLine = sr.ReadLine()) != null)//if the line isn't null
        {
            retval = currentLine.Split(',')[colIndex1Based - 1];//split the line using the delimiter, and pick the column you want.
        }
    }
    return retval;
}