Using SQL stored function gives invalid SQL statement

元气小坏坏 提交于 2019-12-12 17:24:45

问题


I'm trying to use stored function from Oracle Express database in c# application.

OdbcCommand com = new OdbcCommand("SILNIA",sqlConn);
com.CommandType = CommandType.StoredProcedure;

OdbcParameter sqlParam = new OdbcParameter("@ReturnValue", OdbcType.Int);
sqlParam.Direction = ParameterDirection.ReturnValue;
com.Parameters.Add(sqlParam);
com.ExecuteNonQuery();

label1.Content = com.Parameters["@ReturnValue"].Value.ToString();

where SILNIA function (compiles without errors) is defined:

create or replace
FUNCTION SILNIA RETURN NUMBER IS
w NUMBER := 1;
BEGIN
for i in 1..5 loop
w:=w*i;
end loop;

RETURN w;
END SILNIA;

On line:

com.ExecuteNonQuery();

i get

ERROR [42000] [Microsoft][ODBC driver for Oracle][Oracle]ORA-00900: invalid SQL statement.

But this works works without any problems:

OdbcCommand sqlCom = new OdbcCommand("SELECT SILNIA() FROM DUAL", sqlConn);
label1.Content = sqlCom.ExecuteScalar().ToString();

回答1:


You can't use ExecuteNonQuery() to run a SP. You have to do something else -- ExecuteScalar or ExecuteReader

This is because ExecuteNonQuery() is designed to only work with UPDATE, INSERT, and DELETE statements.




回答2:


Your function accepts no parameters - try this:

SELECT SILNIA FROM DUAL


来源:https://stackoverflow.com/questions/4174968/using-sql-stored-function-gives-invalid-sql-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!