Note that there are some explanatory texts on larger screens.

plurals
  1. POADD and DROP SQL Server table constraint command in one transaction caused error
    primarykey
    data
    text
    <p>I have more SQL scripts (each for different version of the application) and I want to execute all scripts in one transaction. </p> <p>I'm executing it from c# using: </p> <pre><code>ExecuteNonQuery(command, conn, trans) </code></pre> <p>on <code>SqlCommand</code>.</p> <p>The SQL commands in the scripts are separated by <code>GO</code> separators. My C# code iterates through all scripts and creates collection of the <code>SqlCommand</code> based on the <code>GO</code> separator. The <code>GO</code> separator is excluded from <code>SqlCommand</code> execution. It is just a separator in script file.</p> <p>All was working fine, but I have found one problem. I have in one script the following command:</p> <pre><code>ALTER TABLE [dbo].[RoleDataPermissions] WITH NOCHECK ADD CONSTRAINT [FK_RoleDataPermissions_OrganizationUnits] FOREIGN KEY([OrganizationUnitID]) REFERENCES [OrganizationUnits] ([ID]) </code></pre> <p>and in the another script (in another version of app this constraint was deleted) I have:</p> <pre><code>ALTER TABLE [dbo].[RoleDataPermissions] DROP CONSTRAINT FK_RoleDataPermissions_OrganizationUnits </code></pre> <p>The first command passed fine, but the second one throws the exception:</p> <blockquote> <p><em>'FK_RoleDataPermissions_OrganizationUnits' is not a constraint. Could not drop constraint. See previous errors.</em></p> </blockquote> <p>I'm trying find out what is causing this problem. I think, that the problem is, that if all commands are executed under one transaction, so the first command is not committed and then the second one cannot find this constraint. I have tried also change the isolation level to <code>readuncommited</code>, but it doesn't help. </p> <p>Do you have any idea how to deal with this?</p> <p>thanks</p>
    singulars
    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.
 

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