全部問候,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)爲:最大,:分.....)
謝謝..
問候, 我試着刪除:在參數定義中的名稱。沒有效果。 我確實使用了ExecuteScaler。如果有可能做我想做的事情,我仍然感興趣(出於好奇)。 該問題似乎與輸出參數定義。當我從查詢中刪除該參數和:theCount時,它就像一個魅力一樣。 – cbeuker 2010-03-25 23:19:50
更新了一個嘗試SELECT INTO – FerranB 2010-03-25 23:41:23
是的,添加開始和結束它,現在它的工作,我可以得到的值作爲輸出參數。謝謝.. – cbeuker 2010-03-26 16:16:42