Solved

Performing Calculations With CF

Posted on 2004-08-17
12
327 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

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.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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