VB6 insert into table from Recordset - vb6

There are one view and one table
both has truly the same columns
but they are in diffrent servers
what I want to do is like below
cn1.ConnectionString = "Server1"
cn2.ConnectionString = "Server2"
sql = "SELECT * FROM VIEW"
Set rs.1ActiveConnection = cn1
rs1.Open sql, cn1
sql = "INSERT INTO table SELECT * FROM view"
cn2.Execute (sql)
I can access to view by cn1, but table by cn2
So this cant be done
I want to know how can it be done
table and view are exactly same
I searched a lot, but there was no good examples for me
I think there are two ways of doing it
inserting recordset into table or inserting each of field to another
easy example would be very helpful thank you

This won't work because, even though you are pulling the records from your first server into your recordset, you are trying to insert directly from the view, rather than inserting from the recordset.
You can loop through your recordset, and insert the records one by one into the other database, but that's a lot of round trips and is very slow. A better way is use UpdateBatch to insert from the recordset all in one go. Here's the basic idea:
cn1.ConnectionString = "Server1"
cn2.ConnectionString = "Server2"
sql = "SELECT * FROM VIEW"
Set rs.ActiveConnection = cn1
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient 'you need a client-side recordset
rs1.Open sql, cn1, adLockBatchOptimistic 'you need to set the locktype to this
Set rs.ActiveConnection = cn2 'now you're connecting your recordset to the other server
rs.UpdateBatch 'and updating the database all in one batch
That should get you started.

Related

Oracle trying to update a table by joining a non indexed table

I tried looking for a similar example to my problem but could not reproduce the solution to my success.
I have 2 tables, Controller and Actions.
The Actions table has the columns Step, Script, Description, Wait_Until and Ref_Code.
The Controller table can only be joined on the Action table by the Ref_Code.
The Action table cannot have a PK because for each Ref_Code there is a Step to be taken.
Im getting an error when trying to update the Controller table using a merge statement:
ORA-30926: unable to get a stable set of rows in the source tables
My merge statement is as follows:
MERGE INTO DSTETL.SHB_FTPS_CONTROLLER ftpsc
USING (SELECT DISTINCT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
sa.step,
sa.next_step,
LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT,
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc, DSTETL.SHB_ACTIONS sa
WHERE SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step) v1
ON (v1.REF_CODE = FTPSC.REF_CODE)
WHEN MATCHED
THEN
UPDATE SET FTPSC.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
ftpsc.next_step = v1.next_step,
ftpsc.curr_step = v1.STEP,
ftpsc.action_script = v1.action_script
WHERE CURRENT_TIMESTAMP >= v1.LAST_ACTION_TMSTMP + v1.WAIT_UNTIL;
COMMIT;
I tried doing this using a normal update as well but Im getting ORA-01732: data manipulation operation not legal on this view.
UPDATE (SELECT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
FTPSC.CURR_STEP,
FTPSC.NEXT_STEP,
FTPSC.ACTION_SCRIPT,
sa.step, --New Step
sa.next_step AS "NNS", --New Next Step
FTPSC.LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT AS "NAS", --New action script
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc
LEFT JOIN
DSTETL.SHB_ACTIONS sa
ON SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step) t
SET t.curr_step = t.step,
t.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
t.next_step = t."NNS",
t.action_script = t."NAS";
COMMIT;
Any advice would be appreciated, I already understand this is because the Action table has multiple Ref_Codes but REF_CODE||STEP is unique. And the output of:
SELECT DISTINCT FTPSC.SESSION_ID,
FTPSC.ORDER_DATE,
sa.step,
sa.next_step,
LAST_ACTION_TMSTMP,
SA.ACTION_SCRIPT,
sa.ref_code,
SA.WAIT_UNTIL
FROM DSTETL.SHB_FTPS_CONTROLLER ftpsc, DSTETL.SHB_ACTIONS sa
WHERE SA.REF_CODE = FTPSC.REF_CODE
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step;
Is how I want the Controller table to be updated like.
Thanks in advance.
It sounds like what you want to do is: update each row in the Controller table with the matching "next step" details from the Actions table. But your Merge statement is querying the Controller table twice, which confuses things.
Is this what you're trying to do?
MERGE INTO DSTETL.SHB_FTPS_CONTROLLER ftpsc
USING (SELECT
step,
next_step,
ACTION_SCRIPT,
ref_code,
WAIT_UNTIL
FROM DSTETL.SHB_ACTIONS
) sa
ON (sa.REF_CODE = FTPSC.REF_CODE)
WHEN MATCHED
THEN
UPDATE SET FTPSC.LAST_ACTION_TMSTMP = CURRENT_TIMESTAMP,
ftpsc.next_step = sa.next_step,
ftpsc.curr_step = sa.STEP,
ftpsc.action_script = sa.action_script
WHERE CURRENT_TIMESTAMP >= ftpsc.LAST_ACTION_TMSTMP + sa.WAIT_UNTIL
AND SA.STEP > ftpsc.curr_step
AND sa.step = ftpsc.next_step;
EDIT: updated query
EDIT2: So, in your original query, in the USING section you were selecting the rows in the Controller table that you wanted to update... but you never joined those rows to the Controller table from the MERGE INTO section to match them up. Having the same alias "ftpsc" just made it less clear that they're two separate objects in the query, and which one you wanted to update.
Honestly I don't really understand why Oracle won't let you update columns that appear in the USING..ON clause. It apparently works fine in SQL Server.

ORA-01461: can bind a LONG value only for insert into a LONG column via Access

I am trying to run the below query in Access on a Oracle database :-
UPDATE tblQuotesNew SET tblQuotesNew.Quote_Status = 'Expired'
WHERE tblQuotesNew.Quote_Status='In Progress' AND tblQuotesNew.Date_Quote_Sent<DateAdd('m',-1,Date())
The data type of Quote_Status is VARCHAR2 size 255. The data type of Date_Quote_Sent is Date.
I am connecting to the Oracle database using the code below :-
Dim mydb As DAO.Database
Dim myq As DAO.QueryDef
connectstring = "ODBC;DSN=Comsales;UID=Comsales;PWD=******;SERVER=PDBREPT"
sqltext = "UPDATE tblQuotesNew SET tblQuotesNew.Quote_Status = 'Expired' WHERE tblQuotesNew.Quote_Status='In Progress' AND tblQuotesNew.Date_Quote_Sent<DateAdd('m',-1,Date());"
myq.ReturnsRecords = False
myq.Connect = connectstring
myq.SQL = sqltext
myq.Execute
myq.Close
When I run this query I get a ORA-01461: can bind a LONG value only for insert into a LONG column error.
You're running an Access query (using Access SQL functions) as a Pass-Through query (by setting connectstring = "ODBC;...).
This won't work. Either use Oracle syntax in a Pass-Through query, or Access syntax in a "regular" Access query.
For the latter, tblQuotesNew must be a linked table, and the query connect string must be empty.

Only 1 row in recordset but all rows in table get updated

The query retrieves a single record as is confirmed by the recordcount but every single row in the table gets updated
I am using vb6 and ms ado 2.8
The Firebird version is 2.5.4.26856 (x64).
Firebird ODBC driver 2.0.3.154
The computer is windows 7 home edition 64 bit
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cs As String
Dim dbPath As String
dbPath = "c:\Parkes\Parkes.fdb"
cs = "DRIVER={Firebird/Interbase(r) Driver}; DBNAME=localhost:" & dbPath & "; UID=SYSDBA; PWD=masterkey;"
cn.ConnectionString = cs
cn.Open
Dim sQuery As String
sQuery = "select memo from clients where clientID = 10021 "
rs.Open sQuery, cn, adOpenStatic, adLockOptimistic
If rs.BOF <> True Or rs.EOF <> True Then
'putting msgbox rs.recordcount here confirms only 1 record in recordset
rs.Movefirst
rs.Fields("memo") = "blah"
rs.Update
End If
Set rs = Nothing
Set cn = Nothing
If I alter the query slightly by also selecting a second column, the client surname then only rows with the same value in the surname column as that of of the row where the clientid is 10021 get edited.
sQuery = "select memo, surname from clients where clientID = 10021 "
I cannot understand how more than one row should be edited when the recordset contains only a single row
EDIT: Having read around the web a bit this is my understanding of what is happening.
It seems that the update method identifies which records to update based on the selected columns in the recordset.
So if you select fields a,b,c,d and are updating field a, it will only update records in the database whose values for a,b,c,d match those in the recordset.
The best way to ensure that you only update a single record is to include the primary key in the selected fields.
So if I had written my query as in the line below, only a single record would have been updated because the clientID column contains unique values.
sQuery = "select memo, clientID from clients where clientID = 10021 "
It makes sense thinking about it but the way I wrote the query originally seems to work fine, in my experience, with other databases or am I wrong?
I tested your code everything was fine and only update one row. I only want to suggest you a simple way to check whether record exist or not. that could be like this:
if rs.rows.count > 0 then
' no need to move recordset to first by default its on the first row
end if

Update value from a select statement

I'm using an Access over Oracle database system (Basically using Access for the forms and getting into the tables using ADO code) and am trying to update a field in the product table with the value of the same named field in a load table.
The code I am using is:
.CommandText = "UPDATE " & strSchema & ".TBL_CAPITAL_MGMT_PRODUCT a INNER JOIN " & strSchema & ".TBL_CAPITAL_MGMT_TEMP_LOAD b ON a.AR_ID = b.AR_ID SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;"
Which returns an error about missing SET keyword.. So I changed it to:
.CommandText = "UPDATE (SELECT a.TOT_RWA_AMT, b.TOT_RWA_AMT As New_RWA_AMT FROM " & strSchema & ".TBL_CAPITAL_MGMT_TEMP_LOAD a INNER JOIN " & strSchema & ".TBL_CAPITAL_MGMT_PRODUCT b ON b.AR_ID = a.AR_ID Where a.New_Rec <> '-1' AND a.IP_ID Is Not Null) c SET c.New_RWA_AMT = c.TOT_RWA_AMT;"
Which returns an error about non key-preserved table. the b table has a pk of AR_ID but the a table has no primary key and it probably won't be getting one, I can't update the structure of any of the tables.
I tried using the /*+ BYPASS_UJVC */ which lets the code run, but doesn't actually seem to do anything.
Anyone got any ideas where I should go from here?
Thanks
Alex
Ignoring the irrelevant ADO code, the update you are trying to do is:
UPDATE TBL_CAPITAL_MGMT_PRODUCT a
INNER JOIN
SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;
This isn't supported by Oracle (though maybe this undocumented BYPASS_UJVC hint is supposed to overcome that, but I wasn't aware of it till now).
Given that your inline view version fails due to lack of constraints you may have to fall back on the traditional Oracle approach using correlated subqueries:
UPDATE TBL_CAPITAL_MGMT_PRODUCT a
SET a.TOT_RWA_AMT = (SELECT b.TOT_RWA_AMT
FROM TBL_CAPITAL_MGMT_TEMP_LOAD b
WHERE a.AR_ID = b.AR_ID
)
WHERE EXISTS (SELECT NULL
FROM TBL_CAPITAL_MGMT_TEMP_LOAD b
WHERE a.AR_ID = b.AR_ID
);
The final WHERE clause is to prevent TOT_RWA_AMT being set to NULL on any "a" rows that don't have a matching "b" row. If you know that can never happen you can remove the WHERE clause.
If you're using Oracle 10g or higher, an alternative to Tony's solution would be to use a MERGE statement with only a MATCHED clause.
MERGE INTO TBL_CAPITAL_MGMT_PRODUCT a
USING TBL_CAPITAL_MGMT_TEMP_LOAD b
ON (a.AR_ID = b.AR_ID)
WHEN MATCHED THEN
UPDATE SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;

SP from SQL Server 2005 is not returning records in recordset (VB6)

Thanks dretzlaff17 for replying,
I am giveing details..........
SP from SQL Server 2005 is not returning the records in recordSet (VB6) records return are -1. If access the records using query and through record set, record set are filling with records.
Same connection string is used. I checked properly and there is no issue in code written in VB6 for command object, then what is the wrong?
Is there any thing else that we have to do while accessing SQL Server 2005.
my code is like
Dim Conn as new ADODB.Connection
Dim RS as new ADODB.RecordSet
Dim CMD as new ADODB.Command
Conn.Open "Connection String" ' Its working
CMD.ActiveConnection = Conn
CMD.CommandType = adCmdStoredProc
CMD.CommandText = "SPName"
Set RS = CMD.Execute
Debug.Print RS.RecordCount ' /* here result is -1 means CMD is not executing and RS is not filling with records */
and if use
RS.Open "Select query", conn 'then this record set is filling with records.
I also check by setting RS (Cursor) location values to client side and SP is simple only select query is present in SP no I/O parameters.
One more thing records are present into database table are not empty.
on this Your thoughts please
Thanks
RS.RecordCount ' /* here result is -1
means CMD is not executing and RS is
not filling with records
No it doesn't: it means the default cursor type does not support the RecordCount property.
As a better test of contents, try:
Debug.Print RS.GetString
Could you show text of you stored proc?
VB6 is old stuff, may be you just need put
set nocount on
at the beginning procedure or may be even execute this as query after opening connection.
If it does not help try simplify stored procedure for something is definitely working like
create stored proc as
set nocount on
select 1
If you want a record count, it's usually best to do this:
set RS = new ADODB.Recordset
RS.Open cmd, , adOpenDynamic, adLockReadOnly
If Not RS.EOF then Debug.Print RS.RecordCount
RS.Close

Resources