Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Format fields as Text or Numeric
Message
De
16/03/2013 11:13:42
 
 
À
15/03/2013 22:20:16
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 7 SP1
OS:
Windows 7
Network:
Windows 2000 Server
Database:
Visual FoxPro
Application:
Desktop
Divers
Thread ID:
01568565
Message ID:
01568593
Vues:
41
If the column is text and is 9 characters wide, then the maximum record count is 999,999,999 - is that enough? Also, if text how is it stored and then to be sorted? If stored as "999 ", then the sorting will be a problem. If you store as " 999", then you are always having to PADR the expression to store and LTRIM to retrieve.

If the column is an Integer, then the number of records is 2**32 - rather large... But then how to sort with other numeric fields? If this is a table of PO line items, then there is the foreign key and the local key - PO Number and Line Number. If both of these are Integers, then the INDEX of ponumber+linenumber would not yield the desired order (it performs a sum of the two expressions and the orders by that. In this case I have used the expression PADL(ponumber,12,"0")+PADL(linenumber,12,"0") to get the desired order. You can have indexes on the fields by themselves, i.e. INDEX ON ponumber TAG ponumber.

In general, I use text fields for keys, but the value is an alpha-numeric value. I start with "00001" and increment to "00009", then continue the increment to "0000A" to "0000Z". Now the next increment is "00010" and this continues -- essentially I am using a base 36 number (0-1, A-Z). So for a field of width=5 the maximum count of results is 36*36*36*36*36 or 60,466,176 values. If I use six digits instead, the value is 2,176,782,336. I find that typically a field width of 5 is sufficient for the maximum record count. The following UDF will get the next sequence number to use (adjust the size of the '00001' value below to return based on your field size):
FUNCTION GetNextId
LPARAMETERS tcAlias, tcField
LOCAL lcNextId, lcCurAlias
lcCurAlias = ALIAS()
SELECT &tcAlias
SET ORDER TO TAG &tcField
GOTO BOTTOM
IF EOF()
	lcNextId = "00001"
ELSE
	lcNextId = INCSEQ(&tcAlias..&tcField)
ENDIF
SET ORDER TO
IF !EMPTY(lcCurAlias)
	SELECT &lcCurAlias
ENDIF
RETURN lcNextId
ENDFUNC


FUNCTION INCSEQ
LPARAMETERS tcLastID
LOCAL lnLen, lcNextID, llNext, lnChr
tcLastID = ALLTRIM(tcLastID)
lnLen    = LEN(tcLastID)
lcNextID = ""
llNext   = True
DO WHILE llNext                                                && Loop to increment decimal places
	IF lnLen > 0
		lnChr = ASC(SUBSTR(tcLastID,lnLen,1))
		IF lnChr>=48 .AND. lnChr<57                            && Check if digit is 0-8
			lnChr = lnChr + 1                                  && Increment digit
			llNext = False
		ELSE
			IF lnChr = 57                                      && Check if digit is 9
				lnChr = 65                                     && Set to character A
				llNext = False
			ELSE
				IF lnChr < 90
					lnChr = lnChr + 1                          && Increment digit
					llNext = False
				ELSE
					lnChr = 48                                 && Set to character 0
				ENDIF
			ENDIF
		ENDIF
		lcNextID = CHR(lnChr) + lcNextID
		lnLen    = lnLen - 1
	ELSE
		lcNextID = "1" + lcNextID
		EXIT
	ENDIF
ENDDO
IF lnLen > 0
	lcNextID = LEFT(tcLastID,lnLen) + lcNextID
ENDIF
RETURN ALLTRIM(lcNextID)
ENDFUNC
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform