HOME | FERGUSON Digital Blog

Using cfstoredproc, cfprocparam, and cfprocresult

ColdFusion , MS SQL2008 Add comments

I recently attended a ColdFusion certification course. One of the things I learned in the course was the use of Stored Procedures(SP). SP are a great way to move your SQL statements out of your code and allows for code re-use. It also takes the strain off your ColdFusion server and forces your SQL server to crunch all the calculations/numbers (which is what it is intended for). This practice will help speed up your process time for your page.

One of the features of the cfstoredproc command is the cfprocresult command. The cfprocresult returns the resultset of your stored procedure as a query. If you have more than one resultset, the cfprocresult has an attribute to set which resultset you want.

For example, you create a stored procedure for finding the employees who are male or female in your organization. The stored procedure might look something like this:

BEGIN
   SET NOCOUNT ON;

   --Get Males

   SELECT   FirstName, LastName
   FROM   TableEmployee
   WHERE   Gender = 'M'

   --Get Females

   SELECT   FirstName, LastName
   FROM   TableEmployee
   WHERE   Gender = 'F'

   END

Within your code on your page, you can call these two results like this:

<cfstoredproc procedure="spEmployeesByGender" datasource="your_datasource">
      <cfprocresult name="Males">
      <cfprocresult name="Females" resultset="2">
   </cfstoredproc>

There is no need to put a resultset on the first cfprocresult. However, if you only wanted to return the Females in this example, you must include the resultset attribute.

You can also pass variables to your stored procedure using the cfprocparam attribute of the cfstoredproc tag. Here is an example of the cfstoredproc tag with cfprocparam and cfprocresult.

<cfstoredproc procedure="spEmployeesByGender" datasource="your_datasource">
      <cfprocparam cfsqltype="CF_SQL_VARCHAR" value="M">   <!--- Send M for Males --->
      <cfprocparam cfsqltype="CF_SQL_VARCHAR" value="F">   <!--- Send F for Females --->   
      <cfprocresult name="MaleEmployees">
      <cfprocresult name="FemaleEmployees" resultset="2">
   </cfstoredproc>

Now you can define vairables within your stored procedure to pass to your query statements.

@MyMaleGender VARCHAR(1), @MyFemaleGender VARCHAR(1)

   BEGIN
   SET NOCOUNT ON;

   --Get Males

   SELECT   FirstName, LastName
   FROM   TableEmployee
   WHERE   Gender = @MyMaleGender

   --Get Females

   SELECT   FirstName, LastName
   FROM   TableEmployee
   WHERE   Gender = @MyFemaleGender

   END

14 responses to “Using cfstoredproc, cfprocparam, and cfprocresult”

  1. Jose Galdamez on I too go with stored procedures whenever possible. Although, there are times where I can't figure out how turn a SQL query into an SP. For example, if you had

    SELECT firstname, lastname
    FROM TableEmployee
    WHERE roleID IN ( 1, 2, 3, 4...)

    I have no idea how you would convert that part in the parentheses to a SP parameter.
  2. Paul Kukiel on You can do lists with stored procs in CF.

    http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

    <cfstoredproc procedure="getAccoutnList" datasource="zzCL" result="storeproc">
    <cfprocresult name="result" >
    <cfprocparam cfsqltype="CF_SQL_VARCHAR" maxlength="100" value="1,2,3">
    </cfstoredproc>
  3. Dustin Brisson on Jose,

    I agree with Paul here. If you already have a CF ValueList, then you can pass it to the SP in the cfprocparam tag. Just make sure you use ## around the list name.

    In your SP, you will need to declare a variable to hold the list (i.e @MyList) then the WHERE clause would be:

    WHERE roleID IN @MyList

    Good luck.
  4. Christian Louboutin Outlet on Awesome post, where is your rss? I cant think it is!
  5. Air Jordan 6 on I think this is one of the so much significant info for me. And im glad reading your article. However wanna statement on some common things, The web site style is perfect, the articles is in reality great . Just right process, cheers.
  6. mortgage loans on If you want to buy a house, you will have to receive the credit loans. Moreover, my mother usually utilizes a collateral loan, which occurs to be really fast.
  7. monsterheadphones on <a href="http://www.salescheapbeatsbydre.com/"><strong>cheap beats by dre</strong></a> This is the interpretation of the article is very good, <a href="http://www.salescheapbeatsbydre.com/"><strong>monster headphones</strong></a> pass this article I read you can have different harvest, <a href="http://www.salescheapbeatsbydre.com/beats-by-dre-dre-lady-gaga-headphones-c-1_23.html"><strong>Dre Lady Gaga Headphones</strong></a> good article introduced to share the happiness, <a href="http://www.salescheapbeatsbydre.com/beats-by-dre-dre-new-style-beats-by-dre-c-1_52.html"><strong>Dre New style Beats By Dre</strong></a> come in and see not cost you too much time but can let you learn a lot<a href="http://www.salescheapbeatsbydre.com/beats-by-dre-dre-beats-by-dre-black-c-1_12.html"><strong>Dre Beats By Dre black</strong></a>
    http://www.salescheapbeatsbydre.com
  8. monsterheadphones on <p><a name="OLE_LINK1"></a><a href="http://www.monsterbeatsbydre-shop.com/"><strong>monster headphones</strong> </a> this is a pattern of <a href="http://www.monsterbeatsbydre-shop.com/"><strong>beats by dre</strong> </a>, design a lot <a href="http://www.monsterbeatsbydre-shop.com/beats-dre-butterfly-c-44.html"><strong>beats dre butterfly</strong> </a>, if has the need to come in the form of reference you need design <a href="http://www.monsterbeatsbydre-shop.com/beats-dre-studio-c-46.html"><strong>beats dre studio</strong> </a>, on this article in general is very good hope everybody many comments <a href="http://www.monsterbeatsbydre-shop.com/dre-beats-studio-gold-monster-headphones-p-86.html"><strong>Dre Beats Studio (Gold) Monster Headphones</strong> </a> </p>
    http://www.monsterbeatsbydre-shop.com
  9. discountnorthface on north face outlet read this article after, cheap north face have a lot of new things is our reality didn't learn north face shop, the article is well written, Mens North Face Apex Bionic key points, concise content Womens North Face Apex Bionic .
    http://www.northfacehonsale.org/
  10. Asics Running Shoes on I will keep reading your blog.
  11. wholesale beads China on Best wishes 2012!
  12. Tom on I inherited a complex (for me anyway) SP. Within the SP, there are some updates and deletes and other updates going on. Ultimately, though, there are 6 resultSets for the cfstoredprcoedure. Let's say I need to make a change to the output for the Resultset=4, how do I know which query in the SP corresponds to resultset=4?
  13. Jordan Air Max on Ultimately, though, there are 6 resultSets for the cfstoredprcoedure. Let's say I need to make a change to the output for the Resultset.
  14. Christian Louboutin Boots on but what can I say?I procrastinate alot and in no way appear to get one thing done.

Leave a Reply




© Copyright 1997-2012, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />