Solved

Performing Calculations With CF

Posted on 2004-08-17
12
295 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
Scale it in WD Gold

With up to ten times the workload capacity of desktop drives, WD Gold hard drives employ advanced technology to deliver among the best in reliability, capacity, power efficiency and performance.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

22 Experts available now in Live!

Get 1:1 Help Now