problem with SQL INSERT [solved]

Post Reply
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

problem with SQL INSERT [solved]

Post by MOISES »

Hi,

I can´t compile this code:

Code: Select all

   

LOCAL cTabla      := company("ITEMS")

  aSql := SQL INSERT INTO &cTabla ( CODIGO, DESCRIPCIO, IMPORTE ) VALUES ( 'C01', 'CONCEPTO PARA PRUEBAS', 12.12 )
   SQLEXEC( aSQL )
 
Thank you.
Last edited by MOISES on Thu Apr 02, 2020 2:59 pm, edited 1 time in total.
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: problem with SQL INSERT

Post by nageswaragunupudi »

Code: Select all

LOCAL cTabla      := company("ITEMS")
Please change it as

Code: Select all

PRIVATE cTabla      := company("ITEMS")
Regards

G. N. Rao.
Hyderabad, India
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Sorry, not working.
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Any fix, please?
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Re: problem with SQL INSERT

Post by Jack »

Hi,
With MSSQL i do this :

csql:="INSERT INTO "+(cTabla)+ " (PATID,LASTNAME) VALUES ("
cSql:=cSql+" 'C01', 'CONCEPTO PARA PRUEBAS'+" )"

TRY
oCon:Execute(cSql)
CATCH oErr
ShowAdoEr( oCon,csql )
END TRY

Hope it will help !

Philippe
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Thank you.

The advantage of such command is that it handles the sintax for every Database. So the same code will work under MSSQL, MySQL, Oracle...
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
User avatar
mgsoft
Posts: 398
Joined: Mon Aug 17, 2009 12:18 pm
Location: España

Re: problem with SQL INSERT

Post by mgsoft »

Maybe this could be:

Code: Select all

#xtranslate ADO_SQL UPDATE  [TABLE <tbl> ] SET [ <col1> = <val1> [,<colN> = <valN> ] ] [ WHERE <*cwhere*> ]  => ;
   "UPDATE " + <tbl> + " SET " + ;
   FW_ArrayAsList( \{  FW_QuotedColSQL( <"col1"> ) + " = " +  FW_ValToSQL( <val1> ) ;
   [,  FW_QuotedColSQL( <"colN"> ) + " = " + FW_ValToSQL( <valN> )  ] \} ) [ + " WHERE " + CharRepl( '"', <"cwhere">, "'", .t. ) ]
Saludos,

Eduardo
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: problem with SQL INSERT

Post by nageswaragunupudi »

MOISES wrote:Sorry, not working.

Code: Select all

   local cSql
   local cName    := "John"
   local cCity    := "New York"
   local dHireDate:= Date() - 1200
   local nSalary  := 123023.75

   PRIVATE cTable := "customer"

   cSql := SQL INSERT INTO &cTable ( FIRST, CITY, HIREDATE, SALARY ) VALUES ( cName, cCity, dHireDate, nSalary )
   ? cSql
 
Image

INSERTING MULTIPLE ROWS:

Code: Select all

   local cSql
   local aData    := ;
      {  { "John",  "New York", Date() - 1200, 23465.55 } ;
      ,  { "Jimmy", "Sydney",   Date() - 2000, 32000.00 } ;
      ,  { "David", "Manila",   Date() - 4000, 54234.76 } ;
      }

   PRIVATE cTable := "customer"

   cSql := SQL INSERT INTO &cTable ( FIRST, CITY, HIREDATE, SALARY ) ARRAY aData
   ? cSql
 
Image
Regards

G. N. Rao.
Hyderabad, India
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Hi,

Hope you are doing well.

There is a typo on calling TABLE. I fixed as follows:

#xtranslate SQL INSERT INTO [TABLE <tbl> ] ( <cols,...> ) ARRAY <a> => ;
"INSERT INTO " + <tbl> + " ( " + FW_QuotedColSQL( #<cols> ) + " ) " + " VALUES " + ;
StrTran( StrTran( FW_ValToSql( AClone( <a> ) ), '( (', '(' ), ') )', ')' )

It now works with local variables.
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: problem with SQL INSERT

Post by nageswaragunupudi »

But we can not change because that breaks the existing code many users are already using.
Regards

G. N. Rao.
Hyderabad, India
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Thank you.

I will create my own function with regular INSERT.

How can I transform all the data in an array into VALUES with FW_ValToSQL?
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: problem with SQL INSERT

Post by nageswaragunupudi »

Please study the existing translates in the fwsqlcmd.ch
Regards

G. N. Rao.
Hyderabad, India
MOISES
Posts: 824
Joined: Wed Aug 22, 2007 10:09 am

Re: problem with SQL INSERT

Post by MOISES »

Thank you. I used FW_AdoApplyParams.

I would like to take this opportunity to thank you for the enormous usefulness of the ADO functions you have created, which make our work much easier. Congratulations once again.
Saludos / Regards,

FWH 20.04, Harbour 3.2.0 dev (r1909261630) y BCC 7.40
Post Reply