Solved

Performing Calculations With CF

Posted on 2004-08-17
12
307 Views
Last Modified: 2013-12-24
Hi

I am trying to update a value in CF by adding 1 to it (or possibly performing other calculations on he value.

The value in the Database is currently 2000 and I am using the following code:

<cfquery name="MyQuery" DATASOURCE="Data">

SELECT * FROM MyTableTBL

</cfquery>

<CFOUTPUT QUERY="MyQuery">
<cfset Claim_Ref=Claim_Ref+'1'>
#claim_ref#
</CFOUTPUT>

The returned value is still only 2000 when it should be 2001, any ideas what I am doing wrong. Sorry I know this is probably simple

Thanks
0
Comment
Question by:stewatts
  • 2
  • 2
  • 2
  • +3
12 Comments
 
LVL 21

Expert Comment

by:pinaldave
ID: 11824287
<CFOUTPUT QUERY="MyQuery">
<cfset Claim_Ref=incrementvalue(Claim_Ref)>
#claim_ref#
</CFOUTPUT>
0
 
LVL 15

Expert Comment

by:tim_cs
ID: 11824325
You are going to have to create a new variable instead of set Claim_Ref
0
 
LVL 15

Expert Comment

by:tim_cs
ID: 11824331
<cfset nClaim_Ref=Claim_Ref+'1'>
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 11824751
tim_cs is right...
The reason it doesn't work is because the value of Claim_Ref is coming from the database, and you can't overwrite the query value inside of the OUTPUT loop, so a new variable is the best option.
You can set the scope of your variable like this to avoid problems also:

<CFOUTPUT QUERY="MyQuery">
  <cfset Variables.Claim_Ref = VAL(Claim_Ref) + 1>
  #Variables.claim_ref#
</CFOUTPUT>
0
 
LVL 21

Expert Comment

by:pinaldave
ID: 11825992
<CFOUTPUT QUERY="MyQuery">
<cfset Claim_Ref_new =i ncrementvalue(Claim_Ref)>
#Claim_Ref_new#
</CFOUTPUT>
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 35

Accepted Solution

by:
mrichmon earned 25 total points
ID: 11826296
Although not really a good way to do it (you should declare a new variable rather than changing a query) you SHOULD be able to do the following :

<cfquery name="MyQuery" DATASOURCE="Data">

SELECT * FROM MyTableTBL

</cfquery>

<CFOUTPUT QUERY="MyQuery">
<cfset MyQuery.Claim_Ref = MyQuery.Claim_Ref + 1>
#claim_ref#
</CFOUTPUT>

Note that I scoped the variable (if you don't scope it CF assumes that it should set a new variable in the VARIABLES scope rather than using the existing one in the query scope.

Also I did not see a reason to have the number 1 in quotes.....
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 11829781
Exactly what mrichmon said...I just wanted to elaborate on is a small bit.

When you loop through a query with cfoutput when you use a variable's value (output it in some cases) it will default to the query's scope. #claim_ref# is the same as #myquery.claim_ref#  this is equally true when using it in cfset when you use the variable  claim_ref its defaulted scope is the query's scope. When you set a variables value it defaults to the variable scope. A better option than trying to remeber any of what I just said is to always scope your variables.
0
 
LVL 6

Expert Comment

by:mosphat
ID: 12064400
Note that MyQuery.Claim_Ref always references the first row, unless you add [currentrow] like this:

<cfoutput query="MyQuery">
<cfset nClaim_Ref = MyQuery.Claim_Ref[currentrow] + 1>
#nClaim_Ref#
</cfoutput>

If you don't want to use an additional variable, there's a function called querySetCell which works like this:

<cfoutput query="MyQuery">
<cfset querySetCell(MyQuery, "Claim_Ref", MyQuery.Claim_Ref[currentrow] + 1, currentrow)>
#Claim_Ref#
</cfoutput>

Then there might be a third, often overlooked option: the database:

select Claim_Ref + 1 as Claim_Ref from tablename

I'm not sure whether you can use it in this case, since it can't be told from the code you gave. But if you can use the database it usually is faster.

0
 
LVL 6

Expert Comment

by:mosphat
ID: 12344707
Tell me you just overlooked my answer, have you? :)
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12344722
With only 25 points to the question the points can only be awarded to one answer - not split.  In taht case the points are awarded to the first correct answer.  If there had been more points I would have definately recommended a split with your answer as it is also helpful.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now