如何从csv文件传递参数到oracle update语句并从csv中排除空值

时间:2014-03-28 21:29:21

标签: oracle shell csv

我有一种情况,我跟随csv文件(比如file.csv),其中包含以下数据:

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

如何使用unix shell循环遍历此文件并说例如列$ 2(Name),我想让所有出现的Name列接受空值并将其传递给例如oracle查询后使用单引号&# 39;'''格式?

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查询和更新查询吗?但同样我不想在其中包含空值。如果列对于我想要排除的所有行完全为空?

1 个答案:

答案 0 :(得分:0)

不确定为什么要在unix shell脚本中循环浏览此文件:也许是因为您无法想到更好的方法?无论如何,我将跳过它并提供纯粹的Oracle解决方案。

我们可以使用外部表格将CSV文件中的数据公开给数据库。这些类似于常规表,除了它们的数据来自数据库服务器上的OS目录中的文件(而不是数据库的存储)。 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