Delete record without leaving orphans

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Delete record without leaving orphans

    I have 2 MS access tables "links" and "linkCategory" where links get
    assigned to a category for display.

    I want to be able to delete a "linkCategory" record but there could be
    "links" records associated with the category so I want to check if there are
    any before deleting the category so that I don't leave orphan links.

    Everything works when I do not query the links table but when I place a
    query to find if there are any links associated with the linkCategory I get
    the following error:

    Error Diagnostic Information
    ODBC Error Code = 37000 (Syntax error or access violation)


    [Microsoft][ODBC Microsoft Access Driver] Syntax error in string in query
    expression 'linkCategoryID=4"'.


    SQL = "select count(*) as liveLink from links where linkCategoryID=4""



    Coding below (the query that is separated by blank lines is the problematic
    one):


    <cfif NOT IsDefined("form.deleteLinkCategory")>
    <cfupdate datasource="#application.dsn#" tablename="linkCategory"
    formfields="linkCategoryID, (remaining fields)......">
    <cfelse>

    <cfquery datasource="#application.dsn#" name="checkForLinks">
    select count(*) as liveLink
    from links
    where linkCategoryID=#Val(linkCategoryID)#"
    </cfquery>
    <cfif #liveLink# is 0>

    <cfquery name="DeleteLinkCategory" datasource="#application.dsn#">
    delete from linkCategory
    where linkCategoryID = #Val(linkCategoryID)#
    </cfquery>
    <cfelse>
    <cfset #errorID#=2>
    <cflocation url="./errorpage.cfm?#errorID#">
    <cfabort>
    </cfif>
    </cfif>



    turtle Guest

  2. Similar Questions and Discussions

    1. Not able to delete record from datagrid
      hi friends, i simply want to delete record by using asp.net datagrid control for that i need to pass one cell value to my function which will...
    2. How do you delete a record form multiple tables
      I have a dbase that has about 30 tables in it. How can I delete a record out of all of the tables without doing 30 DELETE statements? Example:...
    3. #25286 [Csd]: dba_delete() cant delete record when key is null
      ID: 25286 User updated by: audwox at jiran dot com Reported By: audwox at jiran dot com Status: Closed Bug Type: ...
    4. Session variable from delete record page in PHP
      I cannot pass data from delete record page to next page, though session variables works ok on other pages. I found this article...
    5. delete subform record
      Hi, can anyone tell me the best way to delete the current record in a subform, using a command button on a mainform? I know this should be easy,...
  3. #2

    Default Re: Delete record without leaving orphans

    You cannot COUNT a wildcard, nor use wildcards in a query that contains COUNT
    or other such aggregate functions.

    Another option would be to create a custom view that shows related data in
    both tables based on the ID - call your query against the custom View and all
    data in either table should be removed.



    SafariTECH Guest

  4. #3

    Default Re: Delete record without leaving orphans

    >You cannot COUNT a wildcard, nor use wildcards in a query that
    >contains COUNT or other such aggregate functions.
    Glen,

    What do you mean by this? Did I miss something?



    mxstu Guest

  5. #4

    Default Re: Delete record without leaving orphans

    Somebody missed something, possibly me. I thought it was the double quote after the number 4.
    Dan Bracuk Guest

  6. #5

    Default Re: Delete record without leaving orphans

    >Somebody missed something, possibly me. I thought it was the double quote
    >after the number 4.
    If the double quote is in the actual code, then you're right that it might
    cause a problem. I got side tracked by the wildcard comment ;-)



    mxstu Guest

  7. #6

    Default Re: Delete record without leaving orphans

    While I agree that the double quote is the problem, I just thought I would post
    another solution other than having 2 queries.

    delete
    from linkCategory
    where linkCategoryID Not In (Select linkCategoryID from links)
    and linkCategoryID=#Val(linkCategoryID)#

    Ken


    The ScareCrow Guest

  8. #7

    Default Re: Delete record without leaving orphans

    Ken,

    I definitely prefer your "delete" query over the original version. It also
    looks like the OP wants to return some type of status indicating the
    success/failure of the delete operation. Since they are using Access I think
    they may still need an additional query to get the final status of the
    operation... even if it is only using something like @@ROWCOUNT.

    It looks like there are a few other minor issues with the code, like a missing
    query name, extra pound signs, etc.

    <cfif #liveLink# is 0>

    ... should be ....

    <cfif checkForLinks.liveLink eq 0>

    .... and this .....


    <cfelse>
    <cfset #errorID#=2>
    <cflocation url="./errorpage.cfm?#errorID#">
    <cfabort>
    </cfif>


    ...... should probably use a parameter name in the cflocation ....


    <cfelse>
    <cfset errorID =2>
    <cflocation url="./errorpage.cfm?errorID=#errorID#">
    <cfabort>
    </cfif>




    mxstu Guest

  9. #8

    Default Re: Delete record without leaving orphans

    mxstu,

    Yes, I agree. But it is hard to determine what the OP wants to do from this.
    I actually do not like deleting records. While the OP has only listed 2
    tables being linked here, in my experience it usually involves others. Eg you
    might want to keep stats on the links clicked. Then if you delete, then you
    have to delete them all and thus your stats go to crap.
    I prefer to have an extra column (isActive) which is a bit column. This
    allows for the column to be "deleted" (made not active), but still keep all the
    records.

    Ken

    The ScareCrow Guest

  10. #9

    Default Re: Delete record without leaving orphans

    Ken,

    Absolutely. Using an IsActive or IsDeleted flag is they way to go IMO too.
    With "logical" deletes you never have to worry about inadvertently hosing other
    database information. Although, after a few hours scrambling through backups,
    frantically searching for information that was deleted, but shouldn't have
    been, most people become converts too. Experience is a good teacher... bad
    experience is even better ;-)



    mxstu Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139