2013-12-13 26 views
0

我试图选择我的表中的一些值,但是当Invoicedate为空时我得到异常请帮助我!如何通过null值在plsql中选择

我的代码:

query="select vend_name,vend_no from account where inv_date = '"+Invoicedate+"' or vend_no = "+Vendorno+" LIMIT 500"; 

请提供任何可能的答案给我。

+0

你能否详细解释你的需求 – SpringLearner

+0

当然你在传递null的时候会得到一个异常。你在期待什么? –

+0

IIRR Oracle的正确拼写是'foo is NULL' –

回答

1

这是因为在你的代码中它将变成inv_date = 'null'而不是inv_date is null

你需要做的inv_date "+(Invoiceddate==null?"is null":"= '"+Invoiceddate+"'")+" or

一般来说,这是一个糟糕的方式做事情,虽然你是开放的SQL注入攻击和各种其他类似问题。

使用PreparedStatement而这一切会为你处理:

PreparedStatement query = connection.prepareStatement("select vend_name,vend_no from account where inv_date = ? or vend_no = ? LIMIT ?"); 
query.setDate(1, Invoicedate); 
... etc 

还请注意,您应该遵循变量命名等了Java风格的约定,它会帮助人们与您的代码工作。

+1

另请注意'that = NULL'将不会查找空日期请参见[空值](http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements005.htm#SQLRF30037) – GrahamA

+0

确实,比较应该可能是“为空”。 –