Update Does Not Work in VS2010 Using IDB2 - visual-studio-2010

I have no problem when trying to execute and insert or a delete SQL Command. However, this update command does not seems to work well and I am having a hard time to figure it out. Kindly help me please.
I am using an i Series or AS/400 database.
Imports IBM.Data.DB2
Imports IBM.Data.DB2.iSeries
Public conn As New iDB2Connection
Public str As String = "Datasource=10.0.1.11;UserID=edith;password=edith;DefaultCollection=impexplib"
Dim cmdUpdate As New iDB2Command
Dim sqlUpdate As String
conn = New iDB2Connection(str)
conn.Open()
sqlUpdate = "UPDATE impexplib.expusers SET loginDate=#loginDate, loginTime=#loginTime WHERE username=#username"
cmdUpdate.Parameters.Add("username", iDB2DbType.iDB2VarChar)
cmdUpdate.Parameters.Add("loginDate", iDB2DbType.iDB2Date)
cmdUpdate.Parameters.Add("loginTime", iDB2DbType.iDB2Time)
cmdUpdate.Parameters("username").Value = txtUsername.Text
cmdUpdate.Parameters("loginDate").Value = Now.ToString("d")
cmdUpdate.Parameters("loginTime").Value = Now.ToString("T")
cmdUpdate.Connection = conn
cmdUpdate.CommandText = sqlUpdate
cmdUpdate.ExecuteNonQuery()
conn.Close()
Please help me what I am doing wrong? The update code does not really work. Even a simple update of password does not work to.
Thanks!

Assuming no error messages anywhere, if no update is occurring, then the WHERE clause is not being satisfied. Make sure that the user name in DB2 exactly matches the parameter used in the WHERE clause. Very often, DB2 columns are CHAR, not VARCHAR or the other way round. You may also have a situation where the DB2 column is all upper case and the parameter is mixed case. Imagine the DB2 column has "FRED BLOGGS " and your parameter has "Fred Bloggs". This won't satisfy the WHERE clause and no rows will be updated.

Related

How to skip update when parameter is null/0 in spring batch with JdbcBatchItemWriter

We have a scenario wherein while doing a batch update on a table using JdbcBatchItemWriter, We are not finding a way to not update is the attribute is null. We don't want to have too many queries and ItemPreparedStatementSetter so we have a single query to update all fields in the table. Different batch jobs set update different attributes of the table
List<Report> summaryList = getSummaryList()
JdbcBatchItemWriter<ItemMktDcGpReport> writer1 = new JdbcBatchItemWriter<>();
String sql_update = GenericConstants.UPDATE_QUERY;
writer1.setDataSource(dataSource);
ItemPreparedStatementSetter<Report> updatePreparedStatementSetter = new ItemMergeUpdatePreparedItemSetter();
writer1.setItemPreparedStatementSetter(updatePreparedStatementSetter);
writer1.setSql(sql_update);
writer1.afterPropertiesSet();
writer1.write(summaryList);
Tried the following seeing few examples on conditional update at the query but it doesn't help yet.
Below is the query. Any help on this will be very much appreciated.
UPDATE_QUERY = "update [dbo].[test_tbl]
SET test_col1 = CASE When ?!=0 then ?
else test_col1 end ,
test_col2 = CASE When ?!=0 then ?
else test_col2 WHERE market=? and country = ?"
I don't want to construct the SQL query based on parameter as I will lose out on the bulk writing feature of JdbcBatchItemWriter. Can someone please suggest the right approach to solve this problem and possibly correct the SQL query I'm writing?

Oracle database query throws error on second run

i have an VBA code where i am calling oracle to retrieve data twice using ODBC.
First data retrieval is fine. But 2nd time it is saying ,
**RunTime Error '-2147467259 (80004005)'; Unspecified error**
My Code is as follows,
Note: Same code works for connecting Teradata but failed when i use
Oracle
'First Data retrieval
Query1 = "Select TableName from all_tables"
CmdSQLData.CommandText = Query1
CmdSQLData.CommandType = adcmdText
CmdSQLData.Timeout=0
set rs = CmdSQLData.Execute()
'Then code to store data ...
'This part gives proper result ...
rs.close()
'Second Data retrieval
Query2 = "Select * from db.Event"
CmdSQLData.CommandText = Query2
CmdSQLData.CommandType = adcmdText
CmdSQLData.Timeout=0
set rs = CmdSQLData.Execute() 'This line Gives Error - RunTime Error '-2147467259 (80004005)'; Unspecified error
Also i tried creating new command object as cmdSQLData1 but still same
error
May i know why the error is coming for second query ?
There is no problem with query as i have tested in oracle directory.
Please let me know
You won't see this documented much of anywhere, but reusing Command objects with different comamndText is actually a bad practice. You don't say what kind of connection you're using, but for example if it's ODBC, this will internally send a fake invalid SQL to Oracle to force a cleanup of some kind. So instead, throw away your Command object after use and create a new one.
Reusing Command objects is a good practice when you're re-executing the same query with different parameter values, but that's not the case here.
You do not need to use command text at all for those types of queries what you could do is :-
` Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.Open "DSN=Oracle", "User", "Password"
rs.Open "select * from table_a", con
' Read all results
rs.Close
rs.Open "select * from table_b", con
' Read all results
rs.Close
con.Close
You only need to use "Command" if you plan to use a store procedure or a query with bound parameters.

How do I execute a sql statement in lotusscript if the table has a timestamp field/column?

I'm working to retrieve data from oracle 9i to lotus notes. I'm using Oracle 10g for testing since that's what I had. In the default HR database, there is an EMPLOYEES table. I've recently added a new column to get the last modified timestamp which is successful. The data is in the form like this: 25-JUL-12 10.28.32.000000 AM
The following is my lotusscript code:
Option Public
Option Declare
UseLSX "*lsxlc"
%Include "lsconst.lss"
Sub Initialize
Dim s As New NotesSession, db As NotesDatabase
Set db=s.Currentdatabase
Dim lcs As New Lcsession
lcs.Clearstatus
Dim conUser As New Lcconnection("Oracle")
Dim staffdoc As NotesDocument
Dim fieldlistuser$
fieldlistuser="EMPLOYEE_ID,FIRST_NAME,MODIFIED_AT"
conUser.Server="localhost"
conUser.UserID="hr"
conUser.Password="hr"
conUser.Connect
conUser.MetaData="HR.EMPLOYEES"
conUser.Fieldnames=fieldlistuser
Dim fieldsUser As New LCFieldList
Dim fieldUser As LCField
Call conUser.Execute("Select * From HR.EMPLOYEES", fieldsUser)
While conUser.Fetch(fieldsUser) > 0
Set staffdoc=New NotesDocument(db)
staffdoc.Form="Staff"
staffdoc.StaffID=fieldsUser.EMPLOYEE_ID(0)
staffdoc.StaffName=fieldsUser.FIRST_NAME(0)
staffdoc.DateJoin=fieldsUser.MODIFIED_AT(0)
Call staffdoc.Save(True, True)
Wend
conUser.Disconnect
End Sub
Previously everything was ok before I added the timestamp column in the EMPLOYEES table and I can export every row to a temporary lotus view. Now the error always stop at Call conUser.Execute("Select * From HR.EMPLOYEES", fieldsUser). I thought it has something to do with the line fieldlistuser="EMPLOYEE_ID,FIRST_NAME,MODIFIED_AT" so I remove MODIFIED_AT from it and commented staffdoc.DateJoin=fieldsUser.MODIFIED_AT(0) but the error still occurs. The error is Error: Invalid data type for field 'MODIFIED_AT', Connector'Oracle', Method -Execute-. Can this actually be done at all? If can, what datatype in lotus should I store the timestamp value?
My first thought is does the select statement work fine at the server?
Assuming it does, this looks like a problem with the driver converting the timestamp to a Lotus Notes datetime field. As a workaround you could create a stored procedure that returns 'modified at' as a datetime instead of a timestamp type.
Just looking at help docs and I'm wondering if captialization matters, as the LCConnection property I see listed is FieldNames, not Fieldnames.

Oracle db gives ORA-01722 for seemingly NO REASON AT ALL

I'm trying to use an Oracle database with ado.net, and it is proving a painful experience. I use Oracle Client (Oracle.Data namespaces).
The following query runs fine from a query window:
UPDATE PRINT_ARGUMENT
SET VALUE = 'Started'
WHERE REQUEST_ID = 1 AND KEYWORD = '{7D066C95-D4D8-441b-AC26-0F4C292A2BE3}'
When I create an OracleCommand however the same thing blows up with ORA-01722. I can't figure out why.
var cmd = cnx.CreateCommand();
cmd.CommandText = #"
UPDATE PRINT_ARGUMENT
SET VALUE = :value
WHERE REQUEST_ID = :requestID AND KEYWORD = :key";
cmd.Parameters.Add(new OracleParameter("requestID", (long)1);
cmd.Parameters.Add(new OracleParameter("key", "{7D066C95-D4D8-441b-AC26-0F4C292A2BE3}");
cmd.Parameters.Add(new OracleParameter("value", "Started");
cnx.Open();
try { int affected = cnx.ExecuteNonQuery(); }
finally { cnx.Close(); }
When I inspect the command in the debugger, the parameters appear to have mapped to the correct types: requestID has OracleDbType.Int64, key and value are both OracleDbType.Varchar2. The values of the parameters are also correct.
This gets even stranger when you consider that I have other queries that operate on the exact same columns (requestID, keyword, value) using the same approach - and they work without a hiccup.
For the record, the column types are requestID NUMBER(10,0); key VARCHAR2(30); value VARCHAR2(2000).
According to Oracle, ORA-01722 'invalid number' means a string failed to convert to a number. Neither of my string values are numbers, neither of the OracleParameters created for them are numeric, and neither
By default, ODP.NET binds parameters by position, not by name, even if they have actual names in the SQL (instead of just ?). So, you are actually binding requestID to :value, key to :requestID and value to :key.
Correct the order of cmd.Parameters.Add in your code, or use BindByName to tell ODP.NET to use the parameter names.
Since you are using named parameters, you have to tell the Oracle client about it. Otherwise your parameters are mixed up (key is assigned to :value):
OracleParameter parameter = new OracleParameter("requestID", (long)1);
parameter.BindByName = true;
cmd.Parameters.Add(parameter);
It's a strange and unexpected behavior, but that's how it is.

Cannot executing a SQL query through ODP.NET - invalid character error

I'm trying to execute a SQL query through ODP.NET to create a table, but I always get an ORA-00911 'invalid character' error. The Errors object in the exception always has the text "ORA-00911: invalid character\n", even if there are no linebreaks in the SQL query itself.
The code I'm executing the SQL is this:
using (OracleConnection conn = new OracleConnection(<connection string>) {
using (OracleCommand command = conn.CreateCommand()) {
conn.Open();
command.CommandText = queryString;
command.ExecuteNonQuery(); // exception always gets thrown here
}
queryString contains a single CREATE TABLE statement, which works fine when executed through SQL Developer
EDIT: the SQL I am executing is this:
CREATE TABLE "TESTSYNC"."NEWTABLE" (
"COL1" NUMBER(*,0) NULL,
"COL2" NUMBER(*,0) NULL
);
with linebreaks removed
Other people have come across this issue - ODP.NET does not support multiple SQL statements in a text command. The solution is to wrap it in a PL/SQL block with EXECUTE IMMEDIATE around each statement. This lack of support for ; seems incredibly boneheaded to me, and has not improved my opinion of the oracle development team.
Furthermore, this seems to be an issue with oracle itself, as I have the same problems with the MS and ODBC oracle clients.
I had this issue for some reason you have to have code on one line.
I had strSQL = "stuff" +
" more stuff"
I had to put it on one line.
strSQL = "stuff more stuff"
It some how reads the cr/lf.
Wrap your sql in a Begin block.
Dim sqlInsert As String = ""
For i = 1 To 10
sqlInsert += "INSERT INTO MY_TABLE (COUNT) VALUES (" & i & "); "
Next
Call ExecuteSql("BEGIN " & sqlInsert & " END;")
Your quotes are OK (it just forces Oracle to treat your object names as case sensitive i.e. upper case the way you've written it) but I'm not at all sure you're allowed to define NUMBER that way with a *.
I wonder if it is the "*" in the sql have you tried the call without an * in the create? I bet it is yet another "feature" of the ODP.Net driver

Resources