Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take this with a grain of salt: I'm not actually a DBA, and have never worked with SQL Server. However: </p> <p>You're actually up against two different rules here:</p> <ol> <li>Referential constraints</li> <li>Business-specific (I'm assuming) 'delete-allowed' rules.</li> </ol> <p>It sounds like when the referential constraints (in the children tables) were setup, they were created with the option <code>RESTRICT</code> or <code>NO ACTION</code>. This means that attempting to delete from the parent table will, when children rows are present, cause the failure. But the catch is that you want, on a specific table, to allow deletes, and to propogate them (option <code>CASCADE</code>). So, for <em>only</em> those tables where the delete <strong>should</strong> be propogated, alter the referential constraint to use <code>CASCADE</code>. Otherwise, prevent the delete with the (already present) error.</p> <p>As for dealing with the 'exceptions' that crop up... here are some ways to deal with them:</p> <ol> <li>Predict them. Write your delete in such a fashion as to <em>not</em> delete something if the key is referenced in a set of the children tables. This is obviously not maintainable in the long term, or for a heavily-referenced table.</li> <li>Catch the exception. I doubt these can be caught 'in-script' (especially if running a single .txt-type file), so you're probably going to have to at least write a stored procedure, or run from a higher-level language. The second problem here is that you can't 'skip' the error and 'continue' the delete (that I'm aware of) - SQL is going to <em>keep</em> failing you every time (...on what amounts to a random row). You'd have to loop through the <em>entire</em> file, <strong>by line</strong> (or possibly set of lines, dynamically), to be able to ensure the record delete. For a small file (relatively), this <em>may</em> work.</li> <li>Write a (set) of dynamic statements that uses the information schema tables and lookups to exclude ids included in the 'non-deletable' tables. Interesting in concept, but potentially difficult/expensive.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload