LINQ to SQL doesn't call DELETE - sql

I am deleting a bunch of records from different tables that are linked with foreign keys. When I call 'SubmitChanges' the following error is received:
The DELETE statement conflicted with the REFERENCE constraint
FK_PTXProductMap_CustomerProduct". The conflict occurred in database "SOTI", table
"dbo.PTXProductMap", column 'InstanceId'.
The statement has been terminated.
I looked with profiler what queries are executed when SubmitChanges tries to save changes and DELETE SQL operation is not called for 2 records. I 100% sure that linq2sql-'DELETE' operation is called for them (I put a break point to the line:
IEnumerable<CustomerProduct> products = DbContext.CustomerProducts
.Where(cp => cp.InstanceId == transition.InstanceId
|| cp.InstanceId == transition.PreInstanceId);
foreach (CustomerProduct cp in products)
{
DbContext.CustomerProducts.DeleteOnSubmit(cp);
}
and checked if it was called for required cp object. DELETE is called for 2 another record from the same table... but not for all required
Do you have any ideas why this happened? And how to resolve that?
Any ideas are welcome.
P.S. I am working with VS2008 SP1, MS SQL 2005 under 64bit Windows 7
P.P.S. I've detected few records in another table that were linked to the deleted scope... including their deletion resolved current error, but it is still unclear why 'DELETE' operation was not generated for ALL records to be deleted.
P.P.P.S. Pretty similar situation for another pair of table: table A contains 1 record, table B - 3 records that refer to the A.1 They are unable to be deleted in one transaction, but if I delete them manually (through the Management Studio) I am able to delete 3 records from B and then record from A... WHY?

This article about DeleteOnSubmit contains a note dealing with cascade deletes. We recommend you to delete the child rows first, then there should be no problems with deleting parent entities.

Related

Truncating a MS SQL Server Express table with one record takes forever

I am setting up a kind of test database in Microsoft SQL Server Express 2017. I have one main table with 10 columns, which is linked to 6 others, ie its primary key is the foreign key of 6 other tables.
I have populated this main table with just one record.
I need to truncate it - ie delete all the rows but not the table. I tried both truncate table and delete from but both take forever: after 4 minutes the query was still executing! I understand there are keys to check etc, but it's only one record. All the other tables are empty. This doesn't seem right. Any ideas what could be wrong and what I can do to fix it?
In response to the comment
It is probably uncommitted transactions, on your child tables.
You would first be deleting all the child records prior to deleting the parent table record. Is the child tables all empty?.
Do you have any uncommitted transactions in any of those tables? If you do then attempt to kill those sessions by engaging a dba.
A table that has foreign key constraints can't be truncated. Either you drop constraint, truncate table then re-create constraints Or make use of delete cascade. here is a link for same.
TRUNCATE TABLE is a DDL command, it cannot check to see whether the records in the table are being referenced by a record in the child table. In case of DELETE, database is able to make sure that it isn't being referenced by another record.

The DELETE statement conflicted with the REFERENCE constraint, cascading delete

I want to delete a user form 'dbo.Gebruiker' when I run my query I get this error message.
The DELETE statement conflicted with the REFERENCE constraint
"FK_Klant_Gebruiker_beheerderid". The conflict occurred in database
"Planning", table "dbo.Klant", column 'BeheerderId'.
After reading on the forum, they said that first I have to remove from other table so when I run my query, again I get another message
The DELETE statement conflicted with the REFERENCE constraint
"FK_Gebruiker_Klant". The conflict occurred in database "Planning",
table "dbo.Gebruiker", column 'KlantId'.
When I run this query to see if that columns exists
select * from dbo.Gebruiker where
KlantId='1CA25570-1A02-42FC-836D-4897B95EF44A'
it does not show anything.
After reading on google and on forums they say that first I have to delete the foreign key constraint.
I'm also putting the helpConstraint
What would be best way to delete a user from "dbo.Gebruiker" please?
GebruikerTable and Dependencies
KlantTable and Dependencies
In general terms, if you want to delete a row whose id is a FK in another table and you are not using an automated DELETE rule (like CASCADE), then you need to do something with the rows on the dependent table(s) before the database will allow you to perform the DELETE.
It seems that this your problem, so you need to consider what to do with the rows of the dependent table(s) that currently refer to the key value of the row you intend to delete.

Execute an INSERT, UPDATE and DELETE against a SQL Server Database Snapshot

According to http://blogs.msdn.com/b/sqlcat/archive/2011/10/17/updating-a-database-snapshot.aspx I should be able to successfully execute an INSERT, UPDATE and DELETE against a Database Snapshot.
The idea is to create a view of a table before you create the snapshot, and then create the snapshot, and update the View in the snapshot.
I have tried this on my SQL Server 2014 (v12.0.2269) and I still get the error
Failed to update database "Snapshot2015_07" because the database is read-only.
The reason I am keen for this to work is that financials need to be frozen at a particular date, but need to be updated if errors are found in the snapshot.
Has anyone had success recently doing this?
I know there are alternatives like AutoAudit, but it is a lot of work to implement for 1-2 updates/deletes on a database with multiple tables with 5 million + rows
The view has to specify the database name (which is the original database name, not the snapshot database name), along with the schema and table name. Ensure the view you created specifies those three parts of the fully qualified object name.

MS-ACCESS Delete Rows in ODBC Linked Table via "Pass-Through"

I have a a table linked to a ODBC table the ODBC datase Name is CUSTOMER_USER, The table's name is CUSTOMER_INFO, I need to use Pass-through to delete all the records in the ODBC Linked table "CUSTOMER_USER_CUSTOMER_INFO" if the date of thoses records are different from currentdate. I tried the following code AND selected "Pass-Through" in query design tab, but it always return error message
DELETE CUSTOMER_USER_CUSTOMER_INFO.*
FROM CUSTOMER_USER.CUSTOMER_INFO
WHERE SUBMIT_DT <> NOW()
I also tried
DELETE CUSTOMER_USER.CUSTOMER_INFO.*
FROM CUSTOMER_USER.CUSTOMER_INFO
WHERE SUBMIT_DT <> NOW()
or
DELETE CUSTOMER_USER_CUSTOMER_INFO.*
FROM CUSTOMER_USER_CUSTOMER_INFO
WHERE SUBMIT_DT <> NOW()
or even
DELETE CUSTOMER_USER.CUSTOMER_INFO.*
FROM CUSTOMER_USER.CUSTOMER_INFO
None of the above works
Try this? Included a NULL filter as well. NOW() could get problematic. DATE() should not. You might need to shut off the passthrough since DATEVALUE is a JET function though.
DELETE
FROM CUSTOMER_USER_CUSTOMER_INFO
WHERE DATEVALUE(SUBMIT_DT) <> DATE()
AND SUBMIT_DT IS NOT NULL;
I know this is an old thread but I had this problem as well. One thing to be careful of are 1 to many relationships on the SQL Server with the table you are deleting from. If there is a defined 1 to many relationship and you are trying to delete from the 1 side of the relationship, SQL server WILL preserve referential integrity and NOT allow you to delete from the 1 side if there are related records on the Many side of the relationship.
One more tricky thing is that is may not be immediately clear where to find these relationships. You can look in the relationships button on SQL server BUT the problem may also reside in a DELETE trigger for the table on the one side as well. So there are a few places to check. You will either have to modify the DELETE trigger or manually delete the Many side records and then the 1 side records. Good luck.

"select..into" inside Firebird stored procedure

I use Firebird 2.5 64bit edition. I have two tables Master (A) and Detail (B) and I set cascade update and delete for B so if I delete a record in the master any related records in the detail will be deleted as well
I setup an an After Delete trigger for table B that executes and passes parameters to a stored procedure
That stored procedure has this SQL:
select STATUS from A
where A.PK_id = :PK_id
INTO :var_status;
The problem is that I always get NULL for the variable var_status although I checked it in SQL editor and I get 1 which is the correct value, I also checked (using IBexpert debugger) the passed parameter :PK_id and it is also correct!
Why do I get the wrong value stored in this variable.
The probable problem is that you are using AFTER DELETE and the record is not there anymore. Here's the order of actions:
Record gets deleted from A
Deletion from A cascades to deletion on B
The AFTER DELETE trigger in B gets called.
From within the trigger, you try to access data not from B, but from A. It's not there anymore.
Remember that the trigger runs inside a transaction. So, probably, when you run the same SELECT, you can access the value because you are in another transaction and the original transaction has not been committed yet.
This is not so trivial actually. Here are a few options to solve your issue:
Check if your business rules can be changed so that you can run that part of code not in a trigger in B, but in A, where the status will be available. After all, there's something you need to do because of A, not B.
Ultimately, you can remove the cascade delete and handle the deletion of B from within the AFTER DELETE trigger in A. That way, everything will be in the same block of code.