Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to create INSERT with datetime type field?
Message
 
 
À
22/08/2005 09:22:11
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Versions des environnements
Visual FoxPro:
VFP 9
Divers
Thread ID:
01042503
Message ID:
01042647
Vues:
36
>>>
>>>For SQL server see the parametric approach. It also handles headaches for conversion if field on VFP side is either date or datetime. ie:
>>>
>>>
>>>* f1: char, f2: datetime, f3:datetime
>>>cInsertCmd = "insert into myTable (f1, f2, f3) values (?m.Value1, ?m.Value2, ?m.Value3)"
>>>SQLPrepare(m.lnHandle,m.cInsertCmd)
>>>m.ldStart=date()
>>>for ix=1 to 5
>>>  m.Value1 = cdow(m.ldStart+m.ix-1)
>>>  m.VAlue2 = m.ldStart + m.ix - 1
>>>  m.Value3 = datetime() - (m.ix * 3600)
>>>  SQLExec(m.lnHandle)
>>>endfor
Cetin
>>
>>Can you please explain what is the purpose of the "for ix=1" loop? Why do you call SQLExec without an insert command there? Can you do that?
>>
>>Thank you.
>
>for loop is a sample for inserting 5 records. The above command analogy in VFP is:
>
>
>for ix=1 to 5
>  m.Value1 = cdow(m.ldStart+m.ix-1)
>  m.VAlue2 = m.ldStart + m.ix - 1
>  m.Value3 = datetime() - (m.ix * 3600)
>  insert into myTable (f1, f2, f3) values (m.Value1, m.Value2, m.Value3)
>endfor
>
When it's an SQLExec() command you can't simply do this and need a string for 2nd parameter, right? And with SQLserver each time you pass an SQLExec(m.lnHandle, m.lcSQLCommand), m.lcSQLCommand must be compiled before execution. IOW "insert ..." command is first compiled and then executed on SQL server side. If you need multiple inserts with same cInsertCmd skeleton (where only parameter values change) to speed things up you first SQLPrepare(). It does the compilation part. Then you simply call SQLExec(m.lnHandle) within the loop. It is same as:
>
>SQLExec(m.lnHandle,"insert into myTable (f1, f2, f3) values (?m.Value1, ?m.Value2, ?m.Value3)")
>
>but saves recompilation overhead per call. Between calls all you need is to supply the m.Value1...3 (and notice that you simply assign date,datetime etc values as you'd do it in pure VFP). You don't do any conversion for building string. VFP does that for you.
>
>Parametric SQLExec() gets more important when one of the values is a huge string like a memo content. You don't need to build a long string. ie:
>* Assume you have a table in SQL server named "mySQLServerTable" that has employeeID,notes fields.
>
>use employee
>cInsertCmd = "insert into mySQLServerTable (employeeID, notes)"+;
>             " values "+;
>             "(?employee.emp_id,?employee.Notes)"
>SQLPrepare(m.lnHandle,m.cInsertCmd)
>scan
>  SQLExec(m.lnHandle)
>endscan
>SQLExec(m.lnHandle,"select * from mySQLserverTable",'myCursor')
>select myCursor
>browse
Prepare the string with SQLPrepare() and execute per VFP side record within scan..endscan. Though this is not the recommended way to do it for bulk operations, this is a sample why you shouldn't try to convert values in string. Or yet another sample. Suppose you want to store the contents of a file as is on SQL server along with datetime you made the insert:
>
>
>m.lcContents = FileToStr("c:\some path\somefilename.ext")
>m.When = datetime()
>SQLExec(m.lnHandle, ;
>   "insert into myTable (InsertedAt,FileContent) values (?m.When,?m.lcContents)")
>
Cetin,

Thank you very much for the detailed explanation. I appreciate your help very much!
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform