FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour SQL Insert from other table
Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
SQL Insert from other table
Posted: Wed Sep 26, 2012 06:53 AM
Hi all

I have a problem with running SQL with ADO RecordSet.

I have 2 table with same structure, 1 table to temporary use.
( Table1 and Table2 ) structure:
KODE VARCHAR2(5)
KET VARCHAR2(30)

Table1 data:
--------------------
KODE KET
001 AAAAAAAA
002 BBBBBBBB
003 CCCCCCCC

Table2 data: is empty


if i want to add record from table1 to table2, than error

insert into table2 ( select * from table1 ); --> error

but, i do it with long statemen, the result is ok.
ex: insert into TABLE2 (kode,ket) values ('001','test'); --> ok.

this is my code:


Code (fw): Select all Collapse
#include "fivewin.ch"

func main
  local oCon, oRes

  oCon := CREATEOBJECT( "ADODB.Connection" )

  TRY
    oCon:Open( cConnStr )
  CATCH
    MsgInfo( "Error in Opening connection to Oracle" )
    RETURN(.F.)
  END TRY

  Try

    // cSql := "insert into table2 ( select * from table1 where kode='001' )"   // result: error
    // or
    // cSql := "insert into table2 ( select * from table1 )"                            // result: error
    
    cSql  := "insert into TABLE2 (kode,ket) values ('001','test')"    //  result: ok.

    oRes  := FW_OpenRecordSet( oCon, cSQL )
    if oRes = NIL
       msginfo("Error")
    endif

  Catch
    MsgInfo( "Statement error" )
    RETURN(.F.)
  End Try

return nil
// eof


somebody can help me..?



Mulyadi
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 08:06 AM
Mulyadi wrote:insert into table2 ( select * from table1 ); --> error


I never used it. Are you sure it is a correct syntax? Please try it in a SQL tool.

EMG
Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 08:30 AM
Halo, Enrico..
Thanks for reply.

Yes, I try in sql tool with Oracle SQL Developer its fine..
and, if I use this syntax in VS2010 is not problem too.. :-)

Why ... or this a ADO problem...?
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 08:34 AM

I don't know, sorry. :-(

EMG

Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 09:00 AM

Oke, thanks Enrico...
maybe other person can give me a solution..

Mulyadi.

Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 02:10 PM

Halo.... any body help me...?
.
. :)

Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 02:21 PM
It appears you have opened the connection but are mis matching connections with a recordset .. Change this line from
Code (fw): Select all Collapse
oRes  := FW_OpenRecordSet( oCon, cSQL )


To this
Code (fw): Select all Collapse
oCn:Execute( cSQL )


Once you have inserted your records, then create your recordset and view the results.

Rick Lipkin
Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 05:28 PM
Hi,
Thanks Mr. Rick Lipkin.

the solution is works fine.

but I dont understand because sometime in the other case i using FW_OpenRecordSet
and to now it not working and then must be using :Execute
how difference method the two of them..?

Mulyadi.
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 06:02 PM
When you create a connection

Code (fw): Select all Collapse
oCon := CREATEOBJECT( "ADODB.Connection" )

TRY
    oCon:Open( cConnStr )
CATCH
    MsgInfo( "Error in Opening connection to Oracle" )
    RETURN(.F.)
END TRY


It allows you to use SQL Commands such as INSERT,UPDATE,etc ( globally ) on a table.

A Recordset is a 'fetch' of rows with data that can be viewed or manipulated based on a "SELECT" statement

Code (fw): Select all Collapse
oRes  := FW_OpenRecordSet( oCon, cSQL )
xBrowse( oRes )


Hope that helped.

Rick Lipkin
Posts: 85
Joined: Mon Apr 18, 2011 02:32 AM
Re: SQL Insert from other table
Posted: Wed Sep 26, 2012 06:30 PM

Oke Mr..
Thanks for the explanation..
now I understand how must be do so.. :)

Mulyadi.

Continue the discussion