2010-03-25 36 views
1

全部問候,Oracle - 從System.Data.OracleClient獲取Select ...作爲輸出參數(*)

我有一個問題。我正在嘗試構建一個參數化查詢,以獲取Oracle中某個表的行數。相當簡單。但我是一個新手,甲骨文..

我知道在SQL Server中,你可以這樣做:

Select @outputVariable = count(*) from sometable where name = @SomeOtherVariable

,然後你可以設置一個輸出參數在System.Data.SqlClient獲得@outputVariable。

認爲一個人應該能夠做到這一點在Oracle,因此我有以下查詢

Select count(*) into :theCount from sometable where name = :SomeValue

設置我的Oracle參數(使用System.Data.OracleClient的 - 是的,我知道它將在.NET 4中被棄用 - 但是這就是我的工作與現在)如下

IDbCommand command = new OracleCommand(); 
command.CommandText = "Select count(*) into :theCount from sometable where name = :SomeValue"; 
command.CommandType = CommandType.Text; 

OracleParameter parameterTheCount = new OracleParameter(":theCount", OracleType.Number); 
parameterTheCount .Direction = ParameterDirection.Output; 
command.Parameters.Add(parameterTheCount); 

OracleParameter parameterSomeValue = new OracleParameter(":SomeValue", OracleType.VarChar, 40); 
parameterSomeValue .Direction = ParameterDirection.Input; 
parameterSomeValue .Value = "TheValueToLookFor"; 
command.Parameters.Add(parameterSomeValue); 
command.Connection = myconnectionObject; 
command.ExecuteNonQuery(); 
int theCount = (int)parameterTheCount.Value; 

在這一點我希望在數將在參數parameterTheCount,我可以很容易地訪問。

我不斷收到錯誤ora-01036,其中http://ora-01036.ora-code.com告訴我檢查我在sql語句中的綁定。我在SQL語句中搞了些什麼?我在其他地方錯過簡單的東西嗎

我只能使用command.ExecuteScaler(),因爲我只得到一個項目,並且可能最終會使用它,但在這一點上,好奇心已經越來越好了。如果我有兩個參數,我從我的查詢想回來(即:選擇最大(COLA),分(COLB)爲:最大,:分.....)

謝謝..

回答

2

某些版本的ADO不需要冒號:配置OracleParameter

相反的:

new OracleParameter(":theCount", OracleType.Number); 

嘗試

new OracleParameter("theCount", OracleType.Number); 

無論如何,我認爲你必須使用IDbCommandExecuteScalar()功能,避免使用into(這我不知道它是有效的在這方面)。我的意思是:

IDbCommand command = new OracleCommand(); 
command.CommandText = "Select count(*) from sometable where name = :SomeValue"; 
command.CommandType = CommandType.Text; 

OracleParameter parameterSomeValue = new OracleParameter("SomeValue", OracleType.VarChar, 40); 
parameterSomeValue .Direction = ParameterDirection.Input; 
parameterSomeValue .Value = "TheValueToLookFor"; 
command.Parameters.Add(parameterSomeValue); 
command.Connection = myconnectionObject; 
int theCount = (int)command.ExecuteScalar(); 

免責聲明:代碼沒有被編譯,並且可以有任何一個小錯誤。

更新:如果您看一下Oracle SELECT語法,您將看到SELECT INTO句子無法識別。但它在PLSQL語法中是有效的,你可以看到here。你可以試試(未測試)以下,看看它的工作原理之一:

command.CommandText = "begin Select count(*) into :someCount from sometable where name = :SomeValue; end;"; 
+0

問候, 我試着刪除:在參數定義中的名稱。沒有效果。 我確實使用了ExecuteScaler。如果有可能做我想做的事情,我仍然感興趣(出於好奇)。 該問題似乎與輸出參數定義。當我從查詢中刪除該參數和:theCount時,它就像一個魅力一樣。 – cbeuker 2010-03-25 23:19:50

+0

更新了一個嘗試SELECT INTO – FerranB 2010-03-25 23:41:23

+0

是的,添加開始和結束它,現在它的工作,我可以得到的值作爲輸出參數。謝謝.. – cbeuker 2010-03-26 16:16:42

0

我想問題是您在parameterTheCount的參數名稱中有尾隨空格。

編輯

現在請從參數名稱的冒號在構造函數中OracleParameter

+0

對不起,那是我的一個錯字.. – cbeuker 2010-03-25 22:24:38

+0

@cbeuker我已經更新了我的答案 – 2010-03-25 22:36:21

+0

試過了,沒有效果。正如我在對FerranB的評論中所提到的那樣。問題在於定義輸出參數:theCount。 – cbeuker 2010-03-25 23:21:31