ODP.NET ExecuteReader Returns Zero Results When Rows Are Present - oracle

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.

Related

Oracle OJDBC MERGE Statement and Generated Keys

Does Oracle ~>12 support generated keys using a Merge statement? Some sudo code..
MERGE INTO TARGET_TABLE TRG
USING (SELECT CAST(? AS NUMBER) AS ID FROM DUAL) SRC
ON (TRG.ID = SRC.ID)
WHEN MATCHED THEN UPDATE SET....
WHEN NOT MATCHED THEN
INSERT(ID....)
VALUES(MYSEQ.NEXTVAL...)
The prepared statement is set up;
try (PreparedStatement pstmt =
connection.prepareStatement(
loadStatement(sqlName, connection, getClass()), new String[] {ID})) {
...
int inserted = pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
List<Long> keys = new ArrayList<>(inserted);
while (rs.next) {
rs.getLong(1);
}
return keys;
...
I have in-memory tests where the connection uses the H2 driver running the very same SQL and PreparedStatment and that returns the generated key just fine, but Oracle does not.
Reviewing the docs it would suggest it does not?
Thanks!

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

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");

Query results in Oracle SQL Developer are different to those using .Net ODP

I have this query:
SELECT
case
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Postal%')
then 'PostalAddress'
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Email%')
then
'EmailAddress'
else
'OtherAddress'
end as AddressType
FROM VW_Address
WHERE AddressId=190000;
When I execute this query using SQL Developer, the result returned is OtherAddress. This is what I'm expecting. However, when I execute the same query via this code:
Dim cmd =
New Oracle.DataAccess.Client.OracleCommand(["SQL HERE]")
cmd.Parameters.Add(":p0", OracleDbType.Int32)
cmd.Parameters(0).Value = 190000
cmd.BindByName = True
Dim r = cmd.ExecuteReader()
I get a different result: EmailAddress
ODP is version 2.111.7.20, Oracle is 11g.

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);

What is the best way to parameterize a LIKE query?

I'm trying to implement a simple search, using LIKE in my SQL statement:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like '%:text%'"
cmd.Parameters.AddWithValue("text", searchValue)
...
End Using
This doesn't work - what is the best way to parameterize the search value?
select * from TABLE where FIELD like ('%' || :text || '%')
update: my bad, if you are using oracle driver, see this link for help. this depends on which driver you use for db access, in case of oledb you need to do following
here is corrected code:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like ?"
cmd.Parameters.AddWithValue("#text", '%'+searchValue+'%')
...
End Using
in case of oracle driver you need to do following:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like :text"
cmd.Parameters.AddWithValue(":text", '%'+searchValue+'%')
...
End Using
In cases like this I prefer this syntax:
select * from TABLE where INSTR(FIELD, :text) > 0

Resources