SQL Insert from other table

SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 6: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: Select all  Expand view
#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
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am

Re: SQL Insert from other table

Postby Enrico Maria Giordano » Wed Sep 26, 2012 8: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
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 8: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...?
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am


Re: SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 9:00 am

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

Mulyadi.
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am

Re: SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 2:10 pm

Halo.... any body help me...?
.
. :)
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am

Re: SQL Insert from other table

Postby Rick Lipkin » Wed Sep 26, 2012 2:21 pm

It appears you have opened the connection but are mis matching connections with a recordset .. Change this line from
Code: Select all  Expand view

oRes  := FW_OpenRecordSet( oCon, cSQL )
 


To this
Code: Select all  Expand view

oCn:Execute( cSQL )
 


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

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2665
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 5: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.
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am

Re: SQL Insert from other table

Postby Rick Lipkin » Wed Sep 26, 2012 6:02 pm

When you create a connection

Code: Select all  Expand view

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: Select all  Expand view

oRes  := FW_OpenRecordSet( oCon, cSQL )
xBrowse( oRes )
 


Hope that helped.

Rick Lipkin
User avatar
Rick Lipkin
 
Posts: 2665
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: SQL Insert from other table

Postby Mulyadi » Wed Sep 26, 2012 6:30 pm

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



Mulyadi.
User avatar
Mulyadi
 
Posts: 82
Joined: Mon Apr 18, 2011 2:32 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 108 guests