Insert + Select statements in one Query<T> gives ORA-00933: SQL command not properly ended - oracle

I'd like to do a simple INSERT-and-then-SELECT-it-query in one "run" with dapper, roughly like so:
var query = "INSERT INTO Table(Id, Value) Values(1, :value);" +
"SELECT Id, Value FROM Table WHERE Id = 1;"
Connection.Query<Item>(query, new { value = value});
This just gives me ORA-00933: SQL command not properly ended. If I run the exact same query (in "one run", with the params manually replaced) in Oracle SQL Developer, it runs fine.
If I split this in two, and do this:
var insert = "INSERT INTO Table(Id, Value) Values(1, :value);"
var select = "SELECT Id, Value FROM Table WHERE Id = 1;"
Connection.Execute(insert, new { value = value});
var item = Connection.Query<Item>(select);
...it works fine. I've tried removing the last semicolon (which seemed to be a thing in other questions), with no luck.
I've done this exact same thing with success in another project where I'm using SQL Server.
Using Dapper 1.42.
Can anyone see what's wrong here?

Try this. In Insert add "Returning into Clause" and declare output parameters.
var param = new DynamicParameters();
param.Add(name: "IinsValue", value: value, direction: ParameterDirection.Input);
param.Add(name: "Id", dbType: DbType.Int32, direction: ParameterDirection.Output);
param.Add(name: "Value", dbType: DbType.String, direction: ParameterDirection.Output);
Connection.Execute("INSERT INTO Table(Id, Value) Values(1, :value) returning Id,Values into :id, :value", param );
var Id = param.get<int>("Id");
var Val = param.get<String>("Value");

Related

ODP.NET ExecuteReader Returns Zero Results When Rows Are Present

When I execute the following code the value dr.HasRows is false, and dr.Read() returns false. The same query running in SQL Developer returns 10 rows:
var allTablesSql = "select * from all_tables where table_name = 'ADDFIELDSPIPES'";
var cmd = new OracleCommand(allTablesSql, conn);
cmd.CommandType = CommandType.Text;
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
// We have rows!
}
If I change my query to be as follows then dr.HasRows is true and dr.Read() returns true:
var allTablesSql = "select * from all_tables where table_name like '%'";
I suspect that ODP.NET somewhere and somehow queries metadata before executing the SQL string that I supply, and this metadata query somehow prevents ODP.NET from returning any rows for my query.

How to query dataset table on primary key of smallInt using linq C#

Let say I have the following sql table. Customer_id column is a primary key with smallint.
And I am trying to create a linq query, where I want to get all data for customer with id, let say 1.
How to do this.
Already done and not working:
1.
var query = from row in dt.AsEnumerable()
where row.Field<Int32>("customer_id") == Convert.ToInt32(2)
select row;
2.
var query = from row in dt.AsEnumerable()
where row.Field<Int16>("customer_id") == Convert.ToInt16(2)
select row
debug for example 1,2
Syntax error
Exceptions
Why don't you use this:
DataRow needle = hayStack.Tables["Customer"].Rows.Find(2);
Your method should be rewritten as something like this:
private DataRow GetCustomerDetails(Int16 customer_id)
{
return _dt.Tables["Customer"].Rows.Find(customer_id);
}
The calling method would have to check for null beeing returned from the method, since an invalid customer_id would cause Find() tu return null.
Try using short type instead of Int32.

Devart Oracle. Insert and returning ID

We are trying out the Devart Oracle adapter but are having problems with existing code that worked with the Oracle's DataAccess DLL. We get the following example error on command.ExecuteNonQuery():
ORA-01400: cannot insert NULL into
("DatabaseName"."table_name"."col3")
Here is some example code:
const string query = #"INSERT INTO table_name (table_name_id, col1, col2, col3)
VALUES(table_name_id_seq.nextval, :col1, :col2, :col3)
RETURNING table_name_id INTO :output_id";
OracleParameter outputParam = new OracleParameter(":output_id", OracleDbType.Long, ParameterDirection.Output);
OracleParameter[] parameters = new OracleParameter[]
{
outputParam,
new OracleParameter(":col1", OracleDbType.VarChar, col1, ParameterDirection.Input),
new OracleParameter(":col2", OracleDbType.VarChar, col2, ParameterDirection.Input),
new OracleParameter(":col3", OracleDbType.Long, col3, ParameterDirection.Input)
}
using (OracleCommand command = connection.CreateCommand())
{
command.CommandText = query;
command.CommandType = CommandType.Text;
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
int outputId = Convert.ToInt32(outputParam.Value.ToString());
}
What are we doing wrong? We are trying to insert a row using a sequence for the PK, and return the PK for that row all in one query.
Also, the query runs fine if I remove the output parameter and the returning line in the query.
Thank you for your report. We have reproduced the issue and are investigating it. As a workaround, please switch to the Direct Mode: http://www.devart.com/dotconnect/oracle/docs/?directmode.html .

Translate SQL query without FROM clause to LINQ

How to translate query like "select 1, 2" (i.e. without FROM clause) to LINQ statement?
Thanks!
I need to get permissions for a set of user groups. In SQL it looks like
SELECT *
FROM Permission p
INNER JOIN (SELECT GroupID
FROM [Group]
UNION ALL
SELECT 555) AS g
ON (g.GroupID = p.GroupID)
In my case I need to programmatically add a certain code instead "555". I wouldn't like to write special SQL function for that.
I guess you just want to create an anonymous type
var anonymous = new { Column1 = 1, Column2 = 2 };
Edit - Based on Comments
Depending on what your Select projection is you could do something simple like this:
If it is a Int:
var query = (from per in context.permissions
select per).AsEnumerable()
.Concat( new int[] { 1, 2 });
If it is a 'Class'
var query = (from per in context.permissions
select per).AsEnumerable()
.Concat(new CustomClass[]
{
new CustomClass()
{
Prop1= 1
},
}
);
You could also change .Concat to .Union
Why do you need this to be linq?
var numbers = new int[] { 1, 2 };
I suppose
var numbers = Enumerable.Range(1,2);

Oracle bound variables

I'm new to working with bound variables, so I've got a small question -
I'm using an Oracle DB and ODP.NET in a .NET app, and I'm using bound variables like this
string sql = "select * from table1 where loc=:pLoc and pno=:pPno and sno=:pSno union all select * from table2 where loc=:pLoc and pno=:pPno and sno=:pSno union all
select * from table3 where loc=:pLoc and pno=:pPno and sno=:pSno";
OracleCommand _cmd = new OracleCommand(sql, DBFacade.DbConnection);
OracleParameter pLoc = new OracleParameter(":pLoc", OracleDbType.Varchar2, 3);
pLoc.Value = loc;
OracleParameter pSno = new OracleParameter(":pSno", OracleDbType.Varchar2, 10);
pLoc.Value = sno;
OracleParameter pPno = new OracleParameter(":pPno", OracleDbType.Varchar2, 18);
pLoc.Value = pno;
_cmd.Parameters.Add(pLoc);
_cmd.Parameters.Add(pSno);
_cmd.Parameters.Add(pPno);
_odaContractPrices.SelectCommand = _cmd;
I've used bound variables successfully in other sql statements (in simpler queries like "select * from table1 where column1=:param1 and column2=:param2"), but for some reason in this one it doesn't work. Can anyone explain me why ?
Thank you very much!
I'm not sure it's the source of the error but try to remove the : in the OracleParameter constructor and use
_cmd.BindByName = true;
Actually, I think that in your case, the following will suffice:
_cmd.Parameters.Add("pLoc", loc);
_cmd.Parameters.Add("pSno", sno);
_cmd.Parameters.Add("pPno ", pno);

Resources