Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Building UPDATE dynamically
Message
 
 
To
19/03/2014 11:05:40
Dragan Nedeljkovich (Online)
Now officially retired
Zrenjanin, Serbia
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2012
Application:
Web
Miscellaneous
Thread ID:
01596792
Message ID:
01596876
Views:
33
>>What do you see as a downside of this approach?
>
>Too roundabout... you're creating a xml somewhere, then passing that xml with a SP call to SQL, then in SQL extracting the names and values into variables, then composing a statement which would update it... except you can't compose the update statement, it has to be compiled, so you can't pass a variable number of parameters unless you're sure the missing ones will be null... too many steps.
>
>I'd write that as a textmerge to build the update statement with exactly the number of fields you have, dynamically and send it straight to SQL. In metacode,
>
>
lcComma=""
>c=""
>for each oPair in oValues
>   c=c+ lcComma+ opair.name+"="+opair.value  && note that here we need some conversion to string
>   lcComma=", "
>endfor
>text to lcSql noshow textmerge
>update <<lcTable>> set <<c>> where pkField=?nPkValue
>endtext
>nRet=sqlexec(h, lcsql)
>
>where oValues is a collection of objects containing name/value pairs (but you may have them in an array or other storage, so the loop would look differently). The loop could also skip those missing values as well.
>
>Alternately, I'd create an empty object (say, oE) and add properties to it as we go, so the syntax of it would be simpler and no need to convert values into string, VFP+ODBC driver would take care of that:
>
>
>oE=createobject("empty")
>lcComma=""
>c=""
>for each oPair in oValues
>   addproperty(oE, oPair.name, oPair.value)
>   c=c+ lcComma+ opair.name+"=?oE."+opair.name
>   lcComma=", "
>endfor
>text to lcSql noshow textmerge
>update <<lcTable>> set <<c>> where pkField=?nPkValue
>endtext
>nRet=sqlexec(h, lcsql)
>
>
>
>Now if you so strongly prefer stored procedures, enjoy them...

The program I am working on is in C#, not VFP. And I do prefer using stored procedure. Thank you for the sample code.
"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
Previous
Reply
Map
View

Click here to load this message in the networking platform