Oracle 12C:插入值后返回记录

时间:2014-02-10 07:51:53

标签: c# oracle

我想获得在C#中插入表的值,如下所示:

begin
insert into bk_library(floor,section) values('foo2','bar') 
returning id into :outid;
select *from bk_library where id=:outid;
end;

不幸的是,我失败了

  

错误信息:Kiss.Linq.Linq2Sql.Test.EntryPoint.TestInsertReturnId:   Oracle.DataAccess.Client.OracleException:ORA-06550:第3行,列   1:PLS-00428:此SELECT语句中需要INTO子句


    [Test]
    public void TestInsertReturnId()
    {
        int ret = 0;
        string connstring = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=pdborcl)));User Id=system;Password=****;";

        string sql = @"insert into bk_library(floor,section) values('foo','bar') returning id into :outid";
         sql = getSqlString();
        using (DbConnection conn = new OracleConnection(connstring))
        {
            conn.Open();
            DbCommand command = conn.CreateCommand();
            command.CommandType = CommandType.Text;
            command.CommandText = sql;

            OracleParameter lastId = new OracleParameter(":outid", OracleDbType.Int32);
            lastId.Direction = ParameterDirection.Output;
            command.Parameters.Add(lastId);
            ret = command.ExecuteNonQuery();

               // this code work fine ,now I want to get the entire record
            LogManager.GetLogger<EntryPoint>().Info("The new id ={0}", lastId.Value.ToString()); 

            conn.Close();
        }
        Assert.AreNotEqual(ret, 0);
    }

2 个答案:

答案 0 :(得分:0)

ParameterDirection应为ReturnValue

lastId.Direction = ParameterDirection.ReturnValue;

答案 1 :(得分:0)

来自&lt; http://arjudba.blogspot.ch/2008/07/pls-00428-into-clause-is-expected-in.html?m=1&GT;

您需要编写SELECT * INTO some_variable FROM bk_library而不是SELECT * FROM bk_library,因为我假设您要以某种方式存储检索到的数据。因此,您需要声明一个新变量some_variable(我假设类型为字符串)并修改您的SELECT语句,如上所述。然后,语句中的数据将存储在您的新变量中。

希望这有帮助