CementTruck
asked on
Need assistance passing a CF session variable to a SQL stored procedure
Hello Experts!
I have a stored procedure that requires a session variable to run.
1) How do I pass the variable from a cfm page?
2) How do I display the stored procedure's output?
The sp output is ever changing so I can't build an HTML table and hardcode column names as they may change over time. I was hoping for a simple info dump between cfoutput tags.
The SP is already done and works flawlessly in CQL, I just need to display its fruits in CFM.
Thanks in advance!
I have a stored procedure that requires a session variable to run.
1) How do I pass the variable from a cfm page?
2) How do I display the stored procedure's output?
The sp output is ever changing so I can't build an HTML table and hardcode column names as they may change over time. I was hoping for a simple info dump between cfoutput tags.
The SP is already done and works flawlessly in CQL, I just need to display its fruits in CFM.
Thanks in advance!
1) How do I pass the variable from a cfm page?
Call the procedure with cfstoredproc and pass it in as a parameter.
<cfstoredproc procedure="yourProcedureNa me" datasource="....">
<cfprocparam type="IN" value="#session.yourVariab le#"
cfsqltype="(your type)">
<cfprocresult name="theProcResult">
</cfstoredproc>
2) How do I display the stored procedure's output?
You can do it dynamically just like w/a cfquery. Use cfprocresult to capture the output in a query. Then loop through the query columnList to display the results dynamically
<!--- create array of column names --->
<cfset colArray= listToArray(theProcResult. columnList )>
<table>
<cfoutput query="theProcResult">
<tr>
<!--- Requires CF8+ --->
<cfloop array="#colArray#" index="colName">
<td>#theProcResult[colName ][currentR ow]#</td>
</cfloop>
</tr>
</cfoutput>
</table>
Call the procedure with cfstoredproc and pass it in as a parameter.
<cfstoredproc procedure="yourProcedureNa
<cfprocparam type="IN" value="#session.yourVariab
cfsqltype="(your type)">
<cfprocresult name="theProcResult">
</cfstoredproc>
2) How do I display the stored procedure's output?
You can do it dynamically just like w/a cfquery. Use cfprocresult to capture the output in a query. Then loop through the query columnList to display the results dynamically
<!--- create array of column names --->
<cfset colArray= listToArray(theProcResult.
<table>
<cfoutput query="theProcResult">
<tr>
<!--- Requires CF8+ --->
<cfloop array="#colArray#" index="colName">
<td>#theProcResult[colName
</cfloop>
</tr>
</cfoutput>
</table>
ASKER
@_agx_,
We're trying to phase out CF and so we have not spent the $ to continue to upgrade it. We don't have CF8, and if I'm not mistaken, probably have CF6. "Array" is not on the list of delimiters for my cfloop. What else can I try in it's place?
We're trying to phase out CF and so we have not spent the $ to continue to upgrade it. We don't have CF8, and if I'm not mistaken, probably have CF6. "Array" is not on the list of delimiters for my cfloop. What else can I try in it's place?
Then use either a "list" loop
<cfloop list="#theProcResult.colum nList#" index="colName">
<td>#theProcResult[colName ][currentR ow]#</td>
</cfloop>
... or a from/to loop:
<cfloop from="1" to="#arrayLen(colArray)#" index="x">
<td>#theProcResult[colArra y[x]][curr entRow]#</ td>
</cfloop>
> We're trying to phase out CF and so we have not spent
> the $ to continue to upgrade it.
If you have existing apps that would be too expensive to rewrite, might look into the free alternative cfml engines like Railo or OpenBD. You'd get the benefits of the newer features without the cost of upgrades.
<cfloop list="#theProcResult.colum
<td>#theProcResult[colName
</cfloop>
... or a from/to loop:
<cfloop from="1" to="#arrayLen(colArray)#" index="x">
<td>#theProcResult[colArra
</cfloop>
> We're trying to phase out CF and so we have not spent
> the $ to continue to upgrade it.
If you have existing apps that would be too expensive to rewrite, might look into the free alternative cfml engines like Railo or OpenBD. You'd get the benefits of the newer features without the cost of upgrades.
ASKER
@_agx_,
The variable is being passed along just fine. I am now just having issues with the display portion.
I did a cfdump and it did bring back all the info I require, but it isn't in a format I can use.
The variable is being passed along just fine. I am now just having issues with the display portion.
I did a cfdump and it did bring back all the info I require, but it isn't in a format I can use.
ASKER
@_agx_,
I'll try your list loop idea.
I'll try your list loop idea.
> but it isn't in a format I can use.
How so?
How so?
ASKER
@ _agx_,
CFDUMP:
Although the output has all the details I require the columns are not in the right sequence. I need the left column the be the userid column (which is somewhere in the middle column at this point) and the rest of the info from the stored procedure can follow horizontally.
If I could format this the way I need to (color, width, height etc.) the output would be perfect.
LOOP LIST:
Same as above, plus the loop list idea has no table header info.
-------------------------- ---------- ---
If I can just format these to my liking I think we can call this project good.
CFDUMP:
Although the output has all the details I require the columns are not in the right sequence. I need the left column the be the userid column (which is somewhere in the middle column at this point) and the rest of the info from the stored procedure can follow horizontally.
If I could format this the way I need to (color, width, height etc.) the output would be perfect.
LOOP LIST:
Same as above, plus the loop list idea has no table header info.
--------------------------
If I can just format these to my liking I think we can call this project good.
Yeah, columnList is annoyingly always alphabetical. (I wonder who thought that was a good idea).
I don't know if CF6 supports it, but try dumping #getMetaData(theProcResult )#. It should return an array of column props in the sql order: column name, type, ... If not, try this function from cflib http://cflib.org/udf/getQueryColumns. Hopefully one of them should work under 6...
I don't know if CF6 supports it, but try dumping #getMetaData(theProcResult
Let me know which one works, then we can work on column headers.
I can't build an HTML table and hardcode column names as they may
change over time
...
If I could format this the way I need to (color, width, height etc.) the
output would be perfect.
Well the html settings have to come to come from somewhere ;-) Unless you're generating them dynamically inside the proc - you must hard code. ie Set up a structure with the settings to handle columns you DO know about. The rest will just fall through to the defaults.
<cfset prop.columnA.width = 100>
<cfset prop.columnA.color = "ff0000">
<cfset prop.columnB.width = 75>
<cfset prop.columnB.color = "ffff00">
....
<cfoutput query="...">
<cfloop list="#columnNames#" name="colName">
<cfif structKeyExists(prop, colName)>
<td bgColor="#prop[colName].co lor#" ... >
<cfelse>
... column not found. use defaults ....
</cfif>
</cfloop>
</cfoutput>
change over time
...
If I could format this the way I need to (color, width, height etc.) the
output would be perfect.
Well the html settings have to come to come from somewhere ;-) Unless you're generating them dynamically inside the proc - you must hard code. ie Set up a structure with the settings to handle columns you DO know about. The rest will just fall through to the defaults.
<cfset prop.columnA.width = 100>
<cfset prop.columnA.color = "ff0000">
<cfset prop.columnB.width = 75>
<cfset prop.columnB.color = "ffff00">
....
<cfoutput query="...">
<cfloop list="#columnNames#" name="colName">
<cfif structKeyExists(prop, colName)>
<td bgColor="#prop[colName].co
<cfelse>
... column not found. use defaults ....
</cfif>
</cfloop>
</cfoutput>
ASKER
@_agx_,
Thanks for working on solutions for me into the wee hours. Upon returning to work I was pleasantly greeted by this plethora of responses.
To recap:
list loop and from-to loop - both display items from the db. - formatting and column order still required.
#getMetaData(theProcResult )# - surprisingly, this shows me that items #1 and #2 (Columns titled "Fullname" and "UserID") are the first ones that are returned by the query, yet cfdump, list loop and from-to loop all show both these columns scattered throughout the output (?). Fullname is an amalgam of columns titled Firstname and Lastname. I'm wondering if I sneakily retitle these two columns as 1Fullname and 2RecID in the stored procedure that they'll be displayed in the right order (0-9, A-Z)?
Thanks for working on solutions for me into the wee hours. Upon returning to work I was pleasantly greeted by this plethora of responses.
To recap:
list loop and from-to loop - both display items from the db. - formatting and column order still required.
#getMetaData(theProcResult
ASKER
@_agx_,
I am going to have to think about changing my online persona handle to "LoTech" ;) . I changed the column names to aaFullname and aaUserID, and I'll be damned if that didn't handle that issue beautifully. Now for the table headers etc.
I am going to have to think about changing my online persona handle to "LoTech" ;) . I changed the column names to aaFullname and aaUserID, and I'll be damned if that didn't handle that issue beautifully. Now for the table headers etc.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> I'm wondering if I sneakily retitle these two columns as 1Fullname
> and 2RecID in the stored procedure that they'll be displayed in the right
> order (0-9, A-Z)?
Smart thought. That should work. But as long as getMetaData works in MX you don't need it :) You just have to remember cfdump and columnList use alpha order and getMetaData another. For this scenario, getMetaData's order will do nicely.
> and 2RecID in the stored procedure that they'll be displayed in the right
> order (0-9, A-Z)?
Smart thought. That should work. But as long as getMetaData works in MX you don't need it :) You just have to remember cfdump and columnList use alpha order and getMetaData another. For this scenario, getMetaData's order will do nicely.
ASKER
You, Sir, are "The Man!!! (no offense if you're female - insert appropriate title and salutations here to keep me in your good graces).
I have completed the project and have you to thank for it. On to other projects...cheers!
I have completed the project and have you to thank for it. On to other projects...cheers!
Thanks. Glad I could help knock this item off your to-do list :)
ASKER