Submit Your Article Forum Rules

Results 1 to 9 of 9

Thread: Help needed with Cold Fusion code

  1. #1
    Senior Member
    Join Date
    Aug 2006
    Posts
    406
    Can someone tell me what the heck I am doing wrong with this code.

    Thee is no reason for it to work! or is there?

    Code:
    <CFSET email = "">
    <CFSET first_name = "">
    <CFSET RecordCount = 0>
    
    <cfquery name="RemoveRecs" datasource="#request.dsn#">
    		DELETE FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy'; 
    
    	</cfquery>
    
    <cfif RemoveRecs.RecordCount NEQ 0>
    <table border="1" cellpadding="7px">  
      <tr>     
        <th>Email Address</th>   
        <th>First Name</th>   
      </tr>  
      <cfoutput query="remove_recs">
        <tr>       
          <td>#RemoveRecs.email#</td>
          <td>#RemoveRecs.first_name#</td>
        </tr>
      </cfoutput>
    </table>
    <cfelse>
    No Records to delete!
    </cfif>
    
    DONE!
    Any assistance will be appreciated. I have searched the net extensively for help with this.

  2. #2
    Senior Member
    Join Date
    Nov 2003
    Posts
    135
    Quote Originally Posted by jtracking
    Can someone tell me what the heck I am doing wrong with this code.

    Thee is no reason for it to work! or is there?

    ...

    Any assistance will be appreciated. I have searched the net extensively for help with this.
    I'm not sure exactly what you are trying to do but I see two issues right off the bat:

    1) I'm not sure what flavor of database you are talking to but the semi-colon on the end of the delete statement will most likely throw an error.

    2) A SQL delete statement does not return a result set so RemoveRecs.RecordCount will always be zero and RemoveRecs.email & RemoveRecs.first_name will never exist.

    If you are using MSSQL, you can do the following to get the number of rows deleted:

    Code:
    <cfquery name="RemoveRecs" datasource="#request.dsn#"> 
    DELETE FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy'
    select @@RowCount as [RowCount]
       </cfquery>
    The you can use RemoveRecs.RowCount as the number of rows deleted. You will need to do a separate select to get your email and first_name values though.
    --Steve (blog)
    www.shift4.com -- Secure payment processing

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Posts
    135
    In rereading your code, I think what you want is this:

    Code:
    <cftransaction>
      <cfquery name="RemoveRecs" datasource="#request.dsn#"> 
        SELECT email, first_name FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy' 
      </cfquery> 
    
      <cfquery "RemoveRecsCount" datasource="#request.dsn#"> 
        DELETE FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy' 
        select @@RowCount as [RowCount] 
      </cfquery> 
    </cftransaction>
    Now RemoveRecs contains the the result set of the records that were deleted and RemoveRecsCount.RowCount contains the actual number of rows deleted -- which RemoveRecsCount.RowCount "SHOULD" = RemoveRecs.RecordCount
    --Steve (blog)
    www.shift4.com -- Secure payment processing

  4. #4
    Senior Member
    Join Date
    Aug 2006
    Posts
    406
    Well thank you, I will try that and get back to you. Thank you very much !

  5. #5
    Senior Member
    Join Date
    Aug 2006
    Posts
    406
    Ok so, will that display the records that have been deleted? I don't want to run the script until I'm half sure.

  6. #6
    Senior Member
    Join Date
    Nov 2003
    Posts
    135
    Yes, the first cfquery returns a result set with all the records that will match your deletion criteria. The second cfquery actually does the deletion. The cftransaction binds the two queries together so the results of query 1 should match the records deleted in query 2.

    BTW, this really should have been a separate thread... ;)
    --Steve (blog)
    www.shift4.com -- Secure payment processing

  7. #7
    WebProWorld MVP jawn_tech's Avatar
    Join Date
    Jun 2004
    Posts
    1,531
    BTW, this really should have been a separate thread... ;)

    Affirmative. It is now a separate thread.

    :)
    Domain Name Registration and Website Hosting :: DesignerTrade

  8. #8
    Senior Member
    Join Date
    Aug 2006
    Posts
    406
    Quote Originally Posted by Shift4SMS
    In rereading your code, I think what you want is this:

    Code:
    <cftransaction>
      <cfquery name="RemoveRecs" datasource="#request.dsn#"> 
        SELECT email, first_name FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy' 
      </cfquery> 
    
      <cfquery "RemoveRecsCount" datasource="#request.dsn#"> 
        DELETE FROM contacts WHERE email = 'wendyb@mydomain.com' and first_name <> 'Wendy' 
        select @@RowCount as [RowCount] 
      </cfquery> 
    </cftransaction>
    Now RemoveRecs contains the the result set of the records that were deleted and RemoveRecsCount.RowCount contains the actual number of rows deleted -- which RemoveRecsCount.RowCount "SHOULD" = RemoveRecs.RecordCount
    ok so does '@@RowCount' actually mean RemoveRecsCount.RowCount?

  9. #9
    Senior Member
    Join Date
    Nov 2003
    Posts
    135
    @@RowCount is a SQL server variable that contains the number of rows affected by the last SQL statement. In this query, it follows the delete statement so it should reflect the number of rows deleted and it is included in a select statement to return a result set to CF.

    I think the short answer to your question is yes, @@RowCount is RemoveRecsCount.RowCount.
    --Steve (blog)
    www.shift4.com -- Secure payment processing

Similar Threads

  1. ASP.net vs. Cold Fusion (a comparative)
    By fastedge in forum Web Programming Discussion Forum
    Replies: 16
    Last Post: 11-20-2007, 03:12 PM
  2. PHP & Cold Fusion Programmer Needed
    By tameron in forum Services for Sale/Hire
    Replies: 0
    Last Post: 09-01-2007, 03:35 PM
  3. Cold Fusion
    By cooperm in forum Search Engine Optimization Forum
    Replies: 4
    Last Post: 03-09-2005, 06:18 PM
  4. Cold Fusion Datasource Help
    By SusieG in forum Web Programming Discussion Forum
    Replies: 1
    Last Post: 06-30-2004, 08:30 AM
  5. WYSIWYG HTML editor - Cold Fusion
    By blastradius in forum Web Programming Discussion Forum
    Replies: 4
    Last Post: 03-11-2004, 12:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •