有没有办法实现像
这样的简单SELECT INTO
语句
SELECT First_Name
INTO :Name
FROM Employees
WHERE EMPLOYEE_ID = 2
使用ODP.NET返回值参数(“Name”)?
这是我尝试实现它的方法,但OracleCommand抛出1006异常:“ORA-01006:绑定变量不存在”
public void getEmployee(int employee_id)
{
string cmdQuery = @"SELECT First_Name, Email, Salary INTO :FirstName, :EmailAddress, :CurrentSalary FROM EMPLOYEES WHERE employee_id = :Employee_id";
trans_r = conn_r.BeginTransaction();
try
{
OracleCommand cmd = new OracleCommand(cmdQuery, conn_u);
cmd.BindByName = true;
//WHERE Parameters
OracleParameter paramDepartment_Id = new OracleParameter("Employee_id", employee_id);
paramDepartment_Id.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paramDepartment_Id);
//INTO Parameters
OracleParameter param_Name_out = new OracleParameter("FirstName", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Name_out);
OracleParameter param_Email_out = new OracleParameter("EmailAddress", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Email_out);
OracleParameter param_Salary_out = new OracleParameter("CurrentSalary", OracleDbType.Int32, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Salary_out);
OracleDataAdapter da = new OracleDataAdapter(cmd);
OracleCommandBuilder cb = new OracleCommandBuilder(da);
cmd.ExecuteReader();
}
catch (OracleException ex)
{
throw ex;
}
}
答案 0 :(得分:1)
“SELECT INTO”实际上是PL / SQL语言的一部分,而不是SQL。因此,您需要使用匿名PL / SQL块(将其包装在BEGIN和END中)
示例代码: http://oradim.blogspot.com/2007/04/odpnet-tip-anonymous-plsql-and.html
另外,尝试使用ParameterDirection.Output而不是ParameterDirection.ReturnValue
答案 1 :(得分:0)
任何时候ODP.net抛出一个未找到参数的错误,通常意味着参数名称不匹配,参数数量不正确,或者参数格式不正确。参数必须是":PARAM"在param配置中以":"
为前缀 public void getEmployee(int employee_id)
{
string cmdQuery = @"SELECT First_Name, Email, Salary INTO :FirstName, :EmailAddress, :CurrentSalary FROM EMPLOYEES WHERE employee_id = :Employee_id";
trans_r = conn_r.BeginTransaction();
try
{
OracleCommand cmd = new OracleCommand(cmdQuery, conn_u);
cmd.BindByName = true;
//WHERE Parameters
OracleParameter paramDepartment_Id = new OracleParameter(":Employee_id", employee_id);
paramDepartment_Id.Direction = ParameterDirection.Input;
cmd.Parameters.Add(paramDepartment_Id);
//INTO Parameters
OracleParameter param_Name_out = new OracleParameter(":FirstName", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Name_out);
OracleParameter param_Email_out = new OracleParameter(":EmailAddress", OracleDbType.Varchar2, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Email_out);
OracleParameter param_Salary_out = new OracleParameter(":CurrentSalary", OracleDbType.Int32, ParameterDirection.ReturnValue);
cmd.Parameters.Add(param_Salary_out);
OracleDataAdapter da = new OracleDataAdapter(cmd);
OracleCommandBuilder cb = new OracleCommandBuilder(da);
cmd.ExecuteReader();
}
catch (OracleException ex)
{
throw ex;
}
}