2014-03-28 24 views
0

排除空值我有,我有如下csv文件的情況下(比如FILE.CSV)具有以下數據:如何從參數csv文件傳遞到Oracle更新語句,並從CSV

AcctId,Name,OpenBal,closingbal 
1,abc,1000, 
2,,0, 
3,xyz,, 
4,,, 

哪能使用unix shell循環遍歷這個文件並且爲例如列$ 2(Name)說明,我想讓Name列的所有發生都接受空值並將它傳遞給例如下面的帶有單引號'',''格式的oracle查詢?

select * from account 
where name in (collection of values from csv file column name 
       but excluding null values) 
and openbal in 
and same thing for column 3 (collection of values from csv file column Openbal 
          but excluding null values) 
    and same thing for column 4 (collection of values from csv file column 
           closingbal but excluding null values) 

簡而言之,我想將csv列值作爲輸入參數傳遞給oracle sql查詢和更新查詢呢?但是我又不想在其中包含空值。如果一列對於所有行都完全爲空,我也想排除它?

回答

0

不知道爲什麼你想在unix shell腳本中循環訪問這個文件:也許是因爲你想不出更好的方法?無論如何,我會跳過這一點,並提供一個純粹的Oracle解決方案。

我們可以使用外部表格將CSV文件中的數據展示給數據庫。這些數據類似於常規表,除了它們的數據來自數據庫服務器(而不是數據庫存儲)上操作系統目錄中的文件。 Find out more

鑑於這種方法,很容易編寫你想要的查詢。我建議使用子查詢因子從外部表中選擇一次。

with cte as (select name, openbal, closingbal 
       from your_external_tab) 
select * 
from account a 
where a.name in (select cte.name from cte) 
and a.openbal in (select cte.openbal from cte) 
and a.closingbal in (select cte.closingbal from cte) 

IN子句的行爲是從考慮因素中排除NULL。

順便提及,這將返回一個不同的(較大的)結果從該設置:

select a.* 
from account a 
    , your_external_table e 
where a.name = e.name 
and a.openbal= e.openbal 
and a.closingbal = e.closingbal 
+0

之所以採取CSV文件作爲輸入是設計用於用戶的解決方案,因爲用戶被限制運行查詢自己。我們要求用戶將他們指定的參數放在一個csv文件中,並且我們自動將oracle腳本從他們輸入的CSV文件中拾取參數。用戶友好的用戶可以將輸入數據放入CSV文件,這就是爲什麼我們使用這種方法。 – Burair