Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Concatenating strings w/ SQL SELECT
Message
From
03/08/2001 11:08:27
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00539337
Message ID:
00539401
Views:
16
Hi!

Current workarea for SELECT statement is one of the source tables in SQL statement inside of such UDF. However, if this fails (in case current table is Tmp), Name from the second table could be used to see if it is changed. Or just have unique ID field for each names record and use it instead of recno().

I know this is possible, I did t once ;)

Another approach is also to use UDF, but inside UDF do a spearate query that get all text fields by group code, scan the result and prepare the string and return it into the query result. But this work VERY slow (at least in our app) and take a lot of memory (imagine SELECT statement in the duration of the SELECT statement - I wondered how VFP dp not crash with such overload ;).

>I'm not sure that your solution will work. The recno() function in Myfunction returns record number from the current work area and we have no idea what current work area is or could be.
>
>>
>>It could be done, but with certain conditions and with use of custom function (UDF) in the SQL Select statement (and a lot of experimenting). So I would suggest to do what Sergey told, unless you have time to play with this. In addition, cannot be sure that this will work by the same way in further versions of VFP.
>>
>>The condition is that records for each group are selected at once in the table scanning order, and no record from another group between records of one group. You can reach this 2 ways. First is to sort table (index will not help). Second way is select all groups first into the temporary cursor, than select details by joining the source table. During joining process VFP will scan groups table and all records for each group will be joined at once. In other words:
>>
>>
>>SELECT code from MyTable group by code into cursor Tmp
>>selelct t.code, max(MyFunc(s.name,t.code)) as TXTSUM ;
>>  from force Tmp t ;
>>    inner join MyTable s on t.code = s.code
>>  group by t.code
>>
>>
>>For sorting just use SORT VFP command.
>>
>>MyFunc:
>>
>>
>>* initialize these variables before query
>>public LastRecNo, TXTSum, LastCode
>>LastRecNo =0
>>LastCode = 0
>>TXTSum = ''
>>...
>>procedure MyFunc
>>lparameters cName, nCode
>>if recno()<>LastRecNo && this is needed because for first record(s) function called twice
>>                      && not sure how it will work here, maybe you will require to use alias name
>>                      && for recno
>>   if nCode=LastCode
>>     TXTSum = TXTSum+cName
>>   else
>>     TXTSum = cName
>>     LastCode = nCode
>>   endif
>>   LastRecNo = recno()
>>endif
>>endproc
>>
>>
>>>It cannot be done with sql select in VFP but you can do it in the code
>>>
>>>SELECT 0
>>>CREATE CURSOR Mycursor ( Name C(60), code C(1))
>>>
>>>SELECT Mytable
>>>SET ORDER TO code
>>>LOCATE
>>>lcCode = code
>>>lcName = Trim(Name)
>>>DO WHILE NOT EOF()
>>>  SKIP
>>>  SCAN WHILE code = lcCode
>>>    lcName = lcName + ", " + Trim(Name)
>>>  ENDSCAN
>>>  INSERT INTO Mycursor VALUES (lcName, lcCode)
>>>  lcCode = code
>>>  lcName = Trim(Name)
>>>ENDDO
>>>
>>>>I have a table:
>>>>
>>>>Name code
>>>>text1 2
>>>>text2 2
>>>>text3 2
>>>>text4 3
>>>>text5 3
>>>>
>>>>I want to create a cursor gruped by code w/ concatenated Name field:
>>>>
>>>>Cursor1
>>>>Name code
>>>>Text1, Text2, Text3 2
>>>>Text4, Text5 3
>>>>
>>>>but I don't know how:
>>>>
>>>>SELECT ???? AS Name, Code FROM MyTable GROUPED BY MyTable.Code INTO CURSOR Cursor1.
>>>>
>>>>
>>>>TIA
>>>>
>>>>
>>>>Boris
Vlad Grynchyshyn, Project Manager, MCP
vgryn@yahoo.com
ICQ #10709245
The professional level of programmer could be determined by level of stupidity of his/her bugs

It is not appropriate to say that question is "foolish". There could be only foolish answers. Everybody passed period of time when knows nothing about something.
Previous
Reply
Map
View

Click here to load this message in the networking platform