Update records details using Oracle 10g DB and VB.net - oracle

I am trying to figure out how to get some output as to what records were updated when running this query:
UPDATE CSR.TARGET ces
SET (STATUS_CODE, COMPLETE_DATE, DATA) =
(SELECT 'ERROR', '', REPLACE(c.Data, ' x</csr', '</csr')
FROM CSR.TARGET C
WHERE (c.EID = ces.EID)
AND c.STATUS_CODE = 'ERROR')
WHERE EXISTS (SELECT 1
FROM CSR.TARGET C
WHERE (c.EID = ces.EID)
AND c.STATUS_CODE = 'ERROR')
If there are 3 records that were updated by that above query then i would like to know what they were (record ID, etc). How would i go about doing that?
Currently it just tells me 3 records were updated and thats it. No other details.
Any help would be great! Thanks :o)
UPDATE
I am needing this for a query using VB.net so i do not think i can do PL/SQL type of thing?
Dim OracleCommand As New OracleCommand()
Dim ra As Integer
OracleCommand = New OracleCommand("UPDATE CSR.TARGET ces " & _
"SET (STATUS_CODE, COMPLETE_DATE, DATA) = " & _
"(SELECT 'ERROR', '', REPLACE(c.Data, ' x</csr', '</csr') " & _
"FROM CSR.TARGET C " & _
"WHERE (c.EID = ces.EID) " & _
"AND c.STATUS_CODE = 'ERROR') " & _
"WHERE EXISTS (SELECT 1 " & _
"FROM CSR.TARGET C " & _
"WHERE (c.EID = ces.EID) " & _
"AND c.STATUS_CODE = 'ERROR')", OracleConnection)
Try
ra = OracleCommand.ExecuteNonQuery()
OracleConnection.Close()
....
David

You should be able to use the RETURNING clause. Assuming that the EID column is the "record ID" you're referring to and that it is numeric
CREATE OR REPLACE TYPE num_tbl
IS
TABLE OF NUMBER;
DECLARE
l_modified_eids num_tbl;
BEGIN
UPDATE csr.target ces
SET <<omitted>>
WHERE <<omitted>>
RETURNING eid
BULK COLLECT INTO l_modified_eids;
<<Iterate through l_modified_eids to see which rows are modified>>
END;

Based on my reading of that query, all records that have an EID that matches the EID of a record who's status_code is 'ERROR' will be udpated. So if you have a table like this:
ID EID STATUS_CODE
-- --- -----------
1 1 ERROR
2 1 OKAY
3 2 OKAY
both records with ID 1 and 2 would be updated, because 2's EID field matches 1's EID field, and 1's EID field shows error. When it updates, it always uses the data from the row with the 'ERROR'.

Related

Syntax Error in query expression with vb 6.0

sql = "update Attendance set Attended=Attended+1 where Student ID like 15001"
db.Execute (sql)
is is showing :
"syntax error(missing operator) in query expression 'Student ID like
15001'
If realy you have a space between Student and ID tehn your query must be :
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)
sql should always be written with an _. For Example Student ID should always be Student_ID so that it has the idea of reading which column it needs to filter its search. For future purposes always create a column name with an _.
sql = "update Attendance set Attended=Attended+1 where Student_ID like 15001"
db.Execute (sql)
or
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)
Try something that looks a bit cleaner...
sql = "Update tblAttendance SET "
sql = sql & " Attended = Attended + 1 "
sql = sql & " Where Student_ID = 15001 "
db.execute SQL <-- no paranthesis

Read one column values from database and and sort randomly and put the sorted values into other column

I have a requirement to read one complete column values from Access database and need to sort it in a random order/ Descending order and update the sorted values to another column so , that one name is assigned to another name. That to only using VB script.
I know only how to read the values from Access database i don't know further steps..
Could any one help me in this?
One Recordset sort Desc, second Recordset sort Asc. Then loop through the first RS and update the table with the first RS value where table value equals second RS value.
In this example change ColumnName, ColumnName2 and TableName according to your database. ColumnName2 is output column so it will be replaced with new values.
Sub AddDescToAsc()
Dim MyDB As DAO.Database, MyRS As DAO.Recordset, MyRS2 As DAO.Recordset
Set MyDB = CurrentDb()
Set MyRS = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName DESC", dbOpenForwardOnly)
Set MyRS2 = MyDB.OpenRecordset("SELECT ColumnName FROM TableName ORDER BY ColumnName ASC")
MyRS2.MoveFirst
With MyRS
Do While Not .EOF
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE TableName SET ColumnName2 = '" & .Fields("ColumnName") & "' WHERE ColumnName = '" & MyRS2(0) & "'"
DoCmd.SetWarnings True
MyRS2.MoveNext
.MoveNext
Loop
End With
MyRS.Close
MyRS2.Close
End Sub

VB6 Recordsets and SQL count

I am noticing that the number of records in a database table (select reference from datetable) suddenly increases when I start running the program below even though there are no new records added. Please note that I establish that the number of rows increases by running a query in SQL Studio Manager i.e. select reference from datetable. When the program stops; the number of records falls back to the original level. Here is the code. Why does this happen? There is no Primary Key in the table though Reference is unique.
rs.Open "select reference,value1,datefield from datetable where field1 = 'value1' " & _
"order by reference", objAuditCon.ActiveCon, adOpenStatic, adLockPessimistic
Do While Not rs.EOF
intReadCount = intReadCount + 1
DoEvents
If Not IsNull(rs("value1")) Then
testArray = Split(rs("value1"), ",")
rs2.Open "SELECT Date FROM TBL_TestTable WHERE Record_URN = '" & testArray(1) & "'", objSystemCon.ActiveCon, adOpenStatic, adLockReadOnly
If rs2.EOF Then
End If
If Not rs2.EOF Then
rs("DateField") = Format$(rs2("Date"), "dd mmm yy h:mm:ss")
rs.Update
intWriteCount = intWriteCount + 1
End If
rs2.Close
Else
End If
rs.MoveNext
Loop
rs.Close
It's a little confused, but if you are referring to the total given by "intReadCount" against how many rows you have in the table then it looks as though you are not correctly clearing this value. At the beginning of the procedure you would want to set "intReadCount" back to 0 before starting, you should then get constant results.
Updated: See comments below

Use variable to enter into table values

I am unable to use a variable as the vale for a table, though direct values are stored.
I get the error as "Syntax error in INSERT INTO Statement.How do I overcome this ?
sDBPAth = App.Path & "\SETMDBPATH.mdb"
sConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sDBPAth & ";" & _
"Jet OLEDB:Engine Type=4;"
' Type=4 to create an Access97 mdb, If omitted Access2000 mdb
' ------------------------
' Create New ADOX Object
' ------------------------
Set oDB = New ADOX.Catalog
oDB.Create sConStr
Set oCn = New ADODB.Connection
oCn.ConnectionString = sConStr
oCn.Open
Set oCM = New ADODB.Command
oCM.ActiveConnection = oCn
oCM.CommandText = "CREATE TABLE MDBPATH(" & _
"MDBPATH TEXT(40) NOT NULL," & _
"pass TEXT(10))"
oCM.Execute
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES (sDBPAth, 1,)" 'If 'sDBPath' is used the word sDBPath is stored not the variable value
'
' ------------------------
' Release / Destroy Objects
' ------------------------
If Not oCM Is Nothing Then Set oCM = Nothing
If Not oCn Is Nothing Then Set oCn = Nothing
If Not oDB Is Nothing Then Set oDB = Nothing
End Sub
Try
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You had an extra comma at the end, and you need to pass the variable in outside of the string.
You're relatively save in this case since you're not pass in a user input, but if you continue to build INSERT statements like above, you'll be vulnerable to SQL Injection attacks.
You need to amend your SQL statement to allow the application to substitute the sDBPath value
' Populate the table.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
You need to call the variable outside of the string.
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', 1)"
If sDBPAth is an actual VB variable, you need to use something like:
oCn.Execute "INSERT INTO MDBPATH VALUES ('" & sDBPAth & "', '1')"
so that the insert statement is constructed from the value of the variable rather than a fixed string.
In addition, you appear to have a superfluous comma in your statement and you may need quotes around both values since they're both text type.

VBA/ADODB Run-Time Error: 3704

The following VBA subroutine will run most queries just fine. (ie: SELECT * FROM DUAL)
Sub DisplayQuery(QueryString As String)
Dim rs As New ADODB.Recordset
Dim connStr As String
connStr = _
"Provider=MSDAORA.1;" _
& "User ID=abc;Password=123;" _
& "Data Source=xxx/xxx;"
out QueryString
rs.Open QueryString, connStr, adOpenStatic, adLockOptimistic
Range("DataTable").Clear
Cells(1, 1).CopyFromRecordset rs
End Sub
However, when I run the query below, the following error message immediately pops up: Run-time error '3704':Operation is not allowed when the object is closed.
with all_hours as
( select to_date('2009-11-03 05:00 PM','yyyy-mm-dd hh:mi PM') + numtodsinterval(level-1,'hour') hour
from dual
connect by level <= 4 /*hours*/
)
select h.hour
, count(case when h.hour = trunc(s.sampled_on,'hh24') then 1 end) sampled
, count(case when h.hour = trunc(s.received_on,'hh24') then 1 end) received
, count(case when h.hour = trunc(s.completed_on,'hh24') then 1 end) completed
, count(case when h.hour = trunc(s.authorized_on,'hh24') then 1 end) authorized
from all_hours h cross join sample s
group by h.hour
Why?
If I recall correctly, the ADO Command object has a default timeout (30 seconds, I think) which may be causing your problem: there should be a setting like
cn.ConnectionTimeout = (your value here)
which you could extend.
I just restructured my query (link), so that I could put it into a view. I then created a connection in Excel to view name.

Resources