Solved

Performing Calculations With CF

Posted on 2004-08-17
12
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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
 
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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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