Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Convert table structure to create cursor
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00717941
Message ID:
00717958
Views:
14
I see. The following program does exactly that.
*  Program  GCURSDEF.PRG
*  Purpose  Generate cursor definition code 
*  Param's  1 - Cursor Name, 2 - Alias, 3 - prefix for fields to ignore
*   Return  Cursor definition in format of CRETAE CUSRSOR cpmmand
PARAMETERS tcCursorName, tcAlias, tcPref2Ign
PRIVATE lcSrcAlias, lcCursDef, lcCursorName, lnFields, laFields
PRIVATE lcCrLf, lnI, lnMaxLen, lcPref2Ign
DIMENSION laFields[1,4]
IF EMPTY(tcCursorName) OR TYPE("tcCursorName") <> "C"
	WAIT WINDOW "Missing/Incorrect 1st Parameter: Cursor Name"
	RETURN .F.
ENDIF
lcCursorName = ALLTRIM(tcCursorName)
lcSrcAlias = IIF( EMPTY(tcAlias) OR TYPE("tcAlias") <> "C", ;
			ALIAS(), UPPER(ALLTRIM(tcAlias)) )
lcPref2Ign = IIF( EMPTY(tcPref2Ign) OR TYPE("tcPref2Ign") <> "C", ;
				"?", UPPER(tcPref2Ign))
IF EMPTY(lcSrcAlias)
	WAIT WINDOW "Missing/Incorrect 2nd Parameter: Alias"
	RETURN .F.
ENDIF

lcCrLf = CHR(13) + CHR(10)
SELECT (lcSrcAlias)
lnFields = AFIELDS(laFields)
lnMaxLen = 10
FOR lnI=1 TO lnFields
	lnMaxLen = MAX(lnMaxLen, LEN( ALLTRIM(laFields[lnI,1])) )
ENDFOR

lcCursDef = "CREATE CURSOR " + lcCursorName + " ( ;" + lcCrLf
FOR lnI=1 TO lnFields
	* Ignore fields with specified prefix
	IF laFields[lnI,1] = lcPref2Ign
		LOOP
	ENDIF
	lcCursDef = lcCursDef + CHR(9) + ;
					PADR(PROPER(laFields[lnI,1]), lnMaxLen) + ;
					" " + laFields[lnI,2] 
	DO CASE
	CASE INLIST(laFields[lnI,2], "D", "M", "I", "L", "T", "G", "Y")
		*No size requred
	OTHERWISE	
		lcCursDef = lcCursDef + "(" + LTRIM(STR(laFields[lnI,3]))

		IF laFields[lnI,4] > 0
			lcCursDef = lcCursDef + "," + LTRIM(STR(laFields[lnI,4]))
		ENDIF
		lcCursDef = lcCursDef + ")" 
	ENDCASE	

	lcCursDef = lcCursDef + ", ; "  + lcCrLf
	
ENDFOR
lcCursDef = LEFT(lcCursDef, LEN(lcCursDef)-6) + " )"

RETURN lcCursDef
>>You can use SELECT statement to do that.
SEELCT * ;
>>  FROM mytable ;
>>  WHERE 1=2 ;
>>  INTO CURSOR crsMycursor READWRITE
>>
>>>Is there a fast way to create a new cursor by copying the table structure field name and character type. I have a temporary table that has 157 fields, and I now want to use a temporary cursor. However, I have so many fields to define for cursor and was wondering the quickest way to create cursor definition from existing table stucture.
>>>
>>>Thanks
>>>Nick Patel
>
>Thank for replying back Sergey, actually your solution works well, but I am trying to get rid of my temporary table, and I need to dynamically create a cursor as follows:
>
>
>CREATE CURSOR download_table1 (t1_ipin c(10), t1_ccdn1 c(10), t1_ccdn2 c(10), t1_ds d, t1_ts c(4), ;
>					t1_sn c(3), t1_aft c(1), t1_df d, t1_tf c(4), t1_cen c(9), ...
>
>
>is there way I can assembly my cursor (this is a one time thing) without writing out eaching individual field name and type. The field names are a little bit wierd and there a about 157 of them.
>
>Thanks
>Nick
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform