Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
TableUpdate() uses Insert, not Update, with compound key
Message
De
23/11/2011 11:00:24
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Versions des environnements
Visual FoxPro:
VFP 9 SP2
Database:
MS SQL Server
Divers
Thread ID:
01529677
Message ID:
01529699
Vues:
48
Here's my routine that makes the cursor updateable.
LPARAMETERS tcTable, tcAlias, tlHasCalcFields, tcSpecialFields

LOCAL cPK, lPKIsIdentity, lSuccess

lSuccess = .T.

CURSORSETPROP("Tables", "dbo." + m.tcTable, m.tcAlias)
CURSORSETPROP("WhereType", 1, m.tcAlias)

* Find PK
cPK = This.GetPK(m.tcTable)
IF NOT EMPTY(m.cPK)
	CURSORSETPROP("KeyFieldList", m.cPK, m.tcAlias)
	* Only need to check for identity column if
	* the PK is a single field.
	IF NOT "," $ m.cPK
		lPKIsIdentity = This.IsIdentity(m.cPK, m.tcTable)			
	ELSE
		lPKIsIdentity = .F.
	ENDIF 
ENDIF 

LOCAL cFieldList, cUpdateNameList, aFieldList[1], nFields, nField

nFields = AFIELDS(aFieldList, m.tcAlias)
cFieldList = ""
cUpdateNameList = ""

IF m.tlHasCalcFields
	IF SQLCOLUMNS(This.nHandle, m.tcTable, "FOXPRO", "csrColumns") <> 1
		lSuccess = .F.
	ENDIF
ENDIF 
		
IF m.lSuccess

	FOR nField = 1 TO m.nFields
		* Modified 19-October-2011 by TEG
		* If necessary, check whether the field exists in the table.
		IF m.tlHasCalcFields
			SELECT csrColumns
			LOCATE FOR UPPER(csrColumns.Field_Name) = UPPER(aFieldList[m.nField, 1])
			IF NOT FOUND("csrColumns")
				LOOP
			ENDIF 
		ENDIF 

		IF NOT (UPPER(aFieldList[m.nField, 1]) == UPPER(m.cPK)) OR ;
		   NOT m.lPKIsIdentity
			* Don't put identity field in list of updateable fields
			* Wrap field name in brackets in case it's a reserved word.
			cFieldList = m.cFieldList + ", " + aFieldList[m.nField, 1] 
		ENDIF 
		cUpdateNameList = m.cUpdateNameList + ", " + ;
		                  aFieldList[m.nField, 1] + ;
		                  " dbo." + ALLTRIM(m.tcTable) + ".[" + aFieldList[m.nField, 1] + "]"
	ENDFOR 

	cFieldList = SUBSTR(m.cFieldList, 3)
	cUpdateNameList = SUBSTR(m.cUpdateNameList, 3)

	CURSORSETPROP("UpdatableFieldList", m.cFieldList, m.tcAlias)
	CURSORSETPROP("UpdateNameList", m.cUpdateNameList, m.tcAlias)

	CURSORSETPROP("SendUpdates",.T., m.tcAlias)
ENDIF 

RETURN m.lSuccess
It assumes that the original table name and the alias can be different (which is something we need to support, since it's not unusual in this app to pull more than one data set from the same table). The GetPK method queries the information_schema table and builds a comma-separated list of fields marked as primary keys.

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

Click here to load this message in the networking platform