Results 1 to 7 of 7

Thread: Delete SQL stmt

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203

    Delete SQL stmt

    I need to have a SQL stmt wherein if a Publisher Record is Deleted then all related Title Records( holding the Publisher FK) are also Deleted . In MS SQL 7.0 I used to have a stored proc which simply consisted of 2 seperate DELETE SQL stmts. Now Im using Access and Iv tried the following...none work

    1.DELETE FROM Publisher WHERE Publisher.PID='123'
    DELETE FROM Titles WHERE Titles.PID='123'

    2. DELETE FROM Publisher,Titles WHERE Publisher.PID='123' and Titles.PID='123'

    3. DELETE FROM Publisher,Titles WHERE Publisher.PID=Titles.PID and Publisher.PID='123'

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You must delete the records with the FK first. Try your #1 again, but in reverse order. #2 and #3 will not work.
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    OR.... you can set up a foreign key constraint w/ a cascade delete on it.
    Then you only have to delete the parent, and all related FKeys will go w/ it.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203
    This gives a Syntax Error

    DELETE FROM Titles WHERE Titles.PID=2001
    DELETE FROM Publisher WHERE Publisher.PID=2001


    Syntax Error(Missing Operator) in Query expression Titles WHERE Titles.PID=2001 DELETE FROM Publisher WHERE Publisher.PID=2001


    1. I also tried removing the FROM
    2. I tried putting the 2001 as '2001'
    3. I tried Removing the 'Titles.' and the 'Publisher.' from the PID=2001

  5. #5
    Addicted Member
    Join Date
    Jul 2002
    Location
    Grand Rapids, MI
    Posts
    166
    Your sql statement is DELETE FROM Titles WHERE Titles.PID=2001
    DELETE FROM Publisher WHERE Publisher.PID=2001? Are you running two separate queries? Do you get the same error when you run them individually? i guess i just don't understand that error.


    Originally posted by udit99
    This gives a Syntax Error

    DELETE FROM Titles WHERE Titles.PID=2001
    DELETE FROM Publisher WHERE Publisher.PID=2001


    Syntax Error(Missing Operator) in Query expression Titles WHERE Titles.PID=2001 DELETE FROM Publisher WHERE Publisher.PID=2001


    1. I also tried removing the FROM
    2. I tried putting the 2001 as '2001'
    3. I tried Removing the 'Titles.' and the 'Publisher.' from the PID=2001

  6. #6
    Lively Member
    Join Date
    Jun 2002
    Location
    Kuwait
    Posts
    85
    Just try this

    If the datatype for column PID is number then

    Delete * from titles where PID=2001
    Delete * from Publisher where PID=2001

    else if the datatype of PID is text then

    Delete * from titles where PID='2001'
    Delete * from Publisher where PID='2001'
    Your attitude determines your altitude!!!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    203
    Actually the two SQL stmts individually work fine but combined , they give a Syntax error...anyways, I looked for that Cascade Delete feature and I guess this solves my problem without going into the Double SQL stmt bit.

Posting Permissions

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



Click Here to Expand Forum to Full Width