Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Insert into SQL table from VFP table
Message
De
12/06/2003 01:56:40
Dorin Vasilescu
ALL Trans Romania
Arad, Roumanie
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Client/serveur
Divers
Thread ID:
00799236
Message ID:
00799241
Vues:
27
>This is turning out to be much more difficult than it should be: How can I add rows from a VFP table to a SQL table with Insert-SQL? The fieldnames and datatypes in the tables match by design except that the SQL table has an identity column. At the very least I should be able to scan through the VFP rows and insert them one at a time but what is the proper way to reference the VFP data?

I've done this procedure to export my tables to a Firebird database. Maybe if you extract the code that scan the table and execute insert commands to server you can use it.
PROCEDURE dbf2fb
PARAMETERS tcTable, tnConnHnd, tlAddData
*parameters:	tcTable		table name or full path
*				tnConnHnd	valid connection handle returned by SQLConnect()
*				tlAddData	transfer table data?
LOCAL lcCreateTableSQL, lcCreateIndexSQL, lcDropTableSQL, lcInsertDataSQL, ;
		lcVFPTypes, lcFBTypes, lcOptions, lnFields, lnNDX
LOCAL ARRAY laVFPTypes[9], laFBTypes[9], laOptions[9]

USE (tcTable)

*types translation table
lcVFPTypes = 'C^D^L^M^N^F^I^B^Y^T'
lcFBTypes  = 'CHAR^DATE^SMALLINT^BLOB SUB_TYPE TEXT^NUMERIC^FLOAT^INTEGER^DOUBLE PRECISION^NUMERIC^TIMESTAMP'
lcOptions  = 'C^ ^ ^ ^N^N^ ^ ^N^ ^ '
lnFields = AFIELDS(laFlds)
ALINES(laVFPTypes,lcVFPTypes,.t.,'^')
ALINES(laFBTypes,lcFBTypes,.t.,'^')
ALINES(laOptions,lcOptions,.t.,'^')
tcTable = ALIAS()		

*build SQL statements
lcDropTableSQL = [DROP TABLE ]+tcTable				

SET TEXTMERGE on
SET TEXTMERGE TO memvar lcCreateTableSQL NOSHOW 

\\CREATE TABLE <<tcTable>> (
FOR i=1 TO lnFields
	IF i>1
		\\, 
	ENDIF
	lnFieldTypeRange = ASCAN(laVFPTypes, laFlds[i,2])
	\\<<laFlds[i,1]>> <<laFBTypes[lnFieldTypeRange]>>
	IF laOptions[lnFieldTypeRange] = 'N'
		\\(<<laFlds[i,3]>>,<<laFlds[i,4]>>)
	ENDIF
	IF laOptions[lnFieldTypeRange] = 'C'
		\\(<<laFlds[i,3]>>)
	ENDIF
	IF laFlds[i,5] = .f.
		\\ NOT NULL 
	ENDIF
	
NEXT
\\)
SET TEXTMERGE to
SQLEXEC(tnConnHnd,lcDropTableSQL)
SQLEXEC(tnConnHnd,lcCreateTableSQL)

*build and execute SQL statements to add data if third parameter is .T.
IF tlAddData=.t.
	SCAN
		SET TEXTMERGE on
		SET TEXTMERGE TO memvar lcInsertDataSQL NOSHOW 
		\\INSERT INTO <<tcTable>> (
		FOR i=1 TO lnFields
			IF i>1
				\\, 
			ENDIF
			\\<<laFlds[i,1]>>
		NEXT
		\\) VALUES (
		FOR i=1 TO lnFields
			IF i>1
				\\, 
			ENDIF
			DO case
			CASE TYPE(laFlds[i,1]) = 'L'	&&logical type, need conversion
				\\<<IIF(EVALUATE(laFlds[i,1]) = .T.,1,0)>>
			CASE TYPE(laFlds[i,1]) = 'D' OR TYPE(laFlds[i,1]) = 'T' AND EMPTY(EVALUATE(laFlds[i,1]))
	&&empty date, need conversion
				\\'1900-01-01'
			OTHERWISE 
				\\?<<laFlds[i,1]>>
			ENDCASE
		NEXT
		\\)
		SET TEXTMERGE to
		SQLEXEC(tnConnHnd,lcInsertDataSQL)
	ENDSCAN
ENDIF

*build and execute SQL statements to create indexes
lnNDX = ATAGINFO(laIndexes)
FOR i=1 TO lnNDX
	lcIndexName = ALIAS()+'_'+laIndexes[i,1]
	IF laIndexes[i,2] = 'PRIMARY'
		lcCreateIndexSQL = [ALTER TABLE ]+tcTable+[ ADD PRIMARY KEY (]+laIndexes[i,3]+[)]
	ELSE 
		lcCreateIndexSQL = [CREATE INDEX ]+tcTable+[_]+laIndexes[i,1]+[ ON ]+tcTable+[ (]+LaIndexes[i,3]+[)]
	ENDIF
	SQLEXEC(tnConnHnd,lcCreateIndexSQL)
NEXT

RETURN
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform