Steve Yu said:
Can't do another Select on the SPT cursor to another cursor and then Alter Table either
Why are you stating that, of course you can do a select, and then you don't need the alter table. You even have the option to change the SQL of the SQLEXEC to give you a date field in the first place with CONVERT() in T-SQL using this:
Code:
SQLEXEC(handle,'SELECT ...,CONVERT(DATE, fieldname) as fieldname FROM dbo.servrtable ...','resultcursor')
that's the way to convert datetime to date within the SQL Server dialect.
Or directly after the SQLEXEC you do
Code:
SELECT fieldlist, CAST(fieldname as date) as fieldname FROM sptcursor into cursor tempname.
And this is the way to use CAST in VFP sql to do the conversion after you have the cursor.
You surely will depend on the rest of the code acting on the original alias name, so either you select into the same cursor name or you change the name of the SQLEXEC curosr to something temporary and then finally SELECT INTO CURSOR with the alias name you need.
No problem either way. I wonder what you don't have in your own hands in all of this that you say what I quoted. Maybe you didn't understand Mikes suggestion when he said:
Mike Lewis said:
do a SELECT into a new cursor, performing any necessary conversions or other changes along the way.
Performing conversions or adding new fields is always possible with SELECT-SQL and expressions, you can add a new field with CAST(value as type) as fieldname or other expressions and you avoid the ALTER problem, because then you already altered the datatype by the query. "Along the way" is the key here, not afterwards.
It doesn't hurt a cursor to have long field names in itself, it just may lead to the ALTER TABLE not applying to it, but a cursor can have features a free table can't have, that's not the problem, so long field names within the SQL Server table are also not problematic unless you want to finally create a free table from the cursor, but I don't think you need that.
The major point is that you don't need ALTER TABLE at all, because the SQL query can do the field conversions already, also creating new field and even computing new fields like lastname+','+firstname as fullname. Very generally speaking, if you do a query anyway, to get a cursor, SQL is there to give you the result as you need it, not only by joining tables or using group by or other SQL features, also with type conversions. You may need to dig a bit deeper into features available in SQL.-
The rest of your code then can be running untouched, once you fixed the datetime to a date with either already doing the conversion when selecting from the SQL Server table within SQLExec or when you just do a SELECT on the result curosr of the SQLEXEC as described. You have everything in your hands, I think, even if you have to query SELECT * FROM sql_server_view and the view is unchangable to you, you can use a fieldlist including all fields unproblematic and then the CONVERT expression for the datetime field.
You're not bound to anything unless you keep using * as a fieldlist, which is a bad design anyway, even though it is so nicely short and has the aspect of selecting further fields, if the structure of the databasde table or view changes. If you will have a changed structure and use a new field, you have to modify code anyway, and at that time can also add a field to a fieldlist not using the asterisk.
Besides, you can easily get a fieldlist using SQL Server Management studio to create a generic SQL SELECT of a table or view. In the Object Explorer section (typically on the left side), expand the tables node and on the table of interest right click, then pick "Script Table as..." which pops out a secondary context menu where you can pick "...SELECT To" and finally a third popout menu offers "New query editor Window" or simply "clipboard". And that query can be edited with CAST or CONVERT and other expressions making the necessary alterations on the go while you query.
You can use the syntax of the query within the SQLEXEC as is, for example the square brackets around field names. If you want to use the field list for an aftermath SELECT as Mike suggested, you have to strip off the square brackets, of course. But that's also done with two search&replace. It's of course easier when already doing that in the SQL of the SQLEXEC call, I wonder why you wouldn't have that in your hand.
Chriss