Cascading Deletes

May 24, 2004

When I setup a relationship in Access I can specify that Primary Key deletes cascade down to the Forgien Key. So when I delete an Order Header it cleans up all the items in the Order Details table for me automatically.

Can I get this same functionality in SQL Server 7 without having to write triggers or are triggers the only way?

thanks
dog

View 13 Replies


ADVERTISEMENT

Cascading Deletes - Which Is Better - A Trigger Or Foreign Key Cascading Delete?

Aug 17, 2005

I need to implement my cascading deletes on a SQL database.  Is it better (performance/reliablility-wise) to use the Foreign Key Cascading Deletes or to just write my own triggers to do the deletes?I was hoping someone had experimented and found which works best.

View 2 Replies View Related

Cascading Deletes

Dec 21, 1998

I need some suggestions concerning the issue cascading deletes in a self-referencing table, i.e. a table with a foreign key pointing at the primary key in itself. Although SQL Server still does not support cascading deletes the declarative way there are some other ways to handle this situation. One common way is to use trigger coding with the simple structure
CREATE TRIGGER DelCascadeTrig
ON self_ref_tab
FOR DELETE
AS
DECLARE @C_FK xxxxxx
SELECT @C_FK = C_FK FROM DELETED
BEGIN
DELETE self_ref_tab
FROM self_ref_tab, deleted
WHERE self-_ref_tabC_FK = deleted.C_PK
END

where C_PK is the primary key column and C_FK the foreign key column. The problem is that this simple pattern does not work with self-referencing tables, because the removal of dependent rows deeper layer (n-2, n-3 etc. if the originating delete request is level n and the first level of dependant deletes handled by the trigger code is n-1) would require the delete trigger to fire more than once for the same delete operation. As far as I know, in ver. 6.x triggers are executed only once per SQL statement and in this case the n-2, n-3 etc. level rows would have been left as "orphans".

In ver. 7.0 I suppose this should work fine because of the new recursive trigger execution possibility (trigger will fire up to 32 recursive times per SQL statement), but in the meanwhile (i.e. my case util we have upgraded all our servers) the delete logic for a cascading, self-referencing relationship must be handled completely within one execution of the trigger.

My question is now: does anyone know anything about any common algorithm or trigger code example solving this problem.

Thanks in advance

Bjorn Ehnberg

View 1 Replies View Related

Help With Cascading Deletes

Feb 7, 2008

I haven't used cascading deletes in the past but we're starting a new database and it seems like a good way to go to keep data clean. Or at least it did seem like a good way until I ran some tests. I have 3 tables.

People PeopleEmails Email
(pk)peopleid (pk)peopleid (pk)emailid
fname (pk)emailid address
lname emailtype
password

In this structure, the peopleemails table is simply an association table between the email and people table. I have setup up relationships in a diagram so that when a person is deleted, it cascades to peopleemails and removes the entry there. I also had a cascade set up hoping that when an entry was deleted from peopleemails, it would remove it from the email table but this is not happening. The relationship between email and peopleemail is primary key table email.emailid and foreign key table peopleemail.emailid. Is there a way to get this to work to remove the email address if a peopleemail entry is removed? Thanks.

View 5 Replies View Related

Self-Reference Cascading Deletes

Aug 31, 2006

I have the following tableCREATE TABLE [tbl_Items]([item_id] int IDENTITY(1,1) CONSTRAINT PK_tbl_Items__item_idPRIMARY KEY,[parent_id] int DEFAULT(NULL) CONSTRAINTFK_tbl_Items__item_id__parent_id REFERENCES [tbl_Items]( [item_id] ) ONDELETE NO ACTION ON UPDATE NO ACTION)My Intention was to create a table that when I delete a record, allrecords that have on the [parent_id] field the deleted record[item_id].I am trying to avoid having to use triggers or create a storedprocedure that firsts delete the children (recursively) and thendeletes the parent.Is there any way to do this by changing my table definition here?

View 1 Replies View Related

Question About Cascading Deletes

Jul 20, 2005

Hi, I'm using SQL server 2000, and I have set up two tables, table Aand table B. Table A and B have a foreign key constraint such that ifan entry is deleted in table A, then all the entries in table Bassociated with that entry are deleted as well. I imagine that I canfind out the number of records that are deleted in table A when Iexecute the SQL, but is there an easy way of determining the number ofrecords that will be deleted in table B?Thanks,Dan

View 1 Replies View Related

Cascading Updates/Deletes With Triggers

May 19, 2000

Microsoft article Q142480 states "Triggers cannot be used to perform cascading updates and deletes if ForeignKey-to-PrimaryKey relationships have been extablished using SQL Server's DRI."

Does this mean that I cannot declare FK's in my scripts if I want to have triggers in the table? Do I just add a column in my table that will have a foreign key in it, but just not reference it in my script? Can someone clarify for me?

Thanks,
Nathan

View 1 Replies View Related

Peer To Peer Replication And Cascading Deletes

Sep 14, 2007

I was hoping someone could explain how cascading deletes work in Peer to Peer replication on SQL 2005 SP2, the child tables are also being replicated.

View 1 Replies View Related

Cascade Deletes

Jun 11, 2002

Hi,

We are developing a new oltp application using SQL Server 2000 and are debating whether to use "cascade delets" or not. In earlier apps, we had issues using cascade deletes like someone deleted parent by mistake and all child records got deleted OR SQL Server crashed in middle of cascade delete and records were lost or performance became an issue when there were huge # of child records to be deleted,etc.

Are there any recommendations for/against using Cascade deletes and when to use and when NOT to use cascade deletes ?

Thanks

Satish

View 2 Replies View Related

Speedy Deletes

Mar 22, 2007

I'm wondering if anyone has compared the speed of different kinds of deletes.

Assuming super large tables, I wonder, what is the speed ordering for the following operations:
* Remove a column
* Remove a row
* Remove a table

I'll experiment to see if I can get numbers comparing the speeds, but, if someone's already done the work, I'd love to know

thanks

View 3 Replies View Related

Trigger Deletes

Mar 23, 2007

Hello,

I have three tables:

CREATE TABLE TASKS
(
SEQNO INTEGER NOT NULL IDENTITY (10000,1),
NAME VARCHAR(20) DEFAULT '' NOT NULL UNIQUE,

PRIMARY KEY (SEQNO)
);


CREATE TABLE JOBS
(
SEQNO INTEGER NOT NULL IDENTITY (10000,1),
NAME VARCHAR(20) DEFAULT '' NOT NULL UNIQUE,
PRIMARY KEY (SEQNO)
);


CREATE TABLE JOBTASKS
(
SEQNO INTEGER NOT NULL IDENTITY (10000,1),
JOBID INTEGER DEFAULT 0 NOT NULL REFERENCES JOBS(SEQNO),
TASKID INTEGER DEFAULT 0 NOT NULL REFERENCES TASKS(SEQNO),
PRIMARY KEY (SEQNO)
);


When I delete a record in JOBS I would like to create a trigger that would delete the related records in JOBTASKS.

This is my attempt; but it does not work.

CREATE TRIGGER JOBS_BEFORE_DELETE

ON JOBS

FOR DELETE

AS BEGIN

DELETE FROM JOBTASKS

WHERE JOBID = (SELECT SEQNO FROM DELETED);

END;

Thanks,
Steve

View 3 Replies View Related

How Do You Write Multiple DELETES In Same SP

May 23, 2007

I want to use one stored procedure to delete rows in three different tables - how do I write this for Sql Server 2000 please?
PROCEDURE dbo.DeleteSubmission  @C_ID intASDELETE *FROM tblCompaniesWHERE C_ID = @C_ID
DELETE *FROM tblStoresWHERE CS_ID = @C_ID
DELETE *FROM tblDistributorsWHERE CD_ID = @C_ID
RETURN

View 4 Replies View Related

Inserted Row Deletes After Trigger

Oct 22, 2007

I'm hoping someone has seen this before because I have no idea what could be causing it.
 I have an SQL 2005 database with multiple tables and several triggers on the various tables all set to run after insert and update.
My program inserts a record into the "items" via a SP that returns the index of the newly added row. The program then inserts a row into another table that is related to items. When the row is inserted into the second table it gets an error that it cannot insert the record because of a foreign key restraint. Checking the items table shows the record that was just inserted in there is now deleted.
The items record is only deleted when I have my trigger on that table enabled. Here is the text of the trigger:
GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[TestTrigger]ON [dbo].[items]AFTER INSERT
AS BEGIN
SET NOCOUNT ON;
     INSERT INTO tblHistory(table_name, record_id, is_insert)     VALUES ('items', 123, 1)
END
tblHistory's field types are (varchar(50), BigInt, bit).
As you can see there is nothing in the trigger to cause the items record to be deleted, so I have no idea what it could be? Anyone ever see this before?
Thanks in advance!

View 7 Replies View Related

Cascade Deletes In SQL 2005

Jun 14, 2006

I have a logins table, a loginroles table (intermediate), and a roles table
When I delete a login from logins I need to delete the roles for that login from loginroles. I know I have to use cascade deletes, but I cannot find the option in sql 2005.
Any help would be appreciated.
Thank You,Jason

View 3 Replies View Related

Cmdshell ... And Deletes Dos Files ?

Jun 21, 2001

hello,
Can you give me an example of suppression under DOS, of the files with under-directories by exec cmdshell
thank you in advance
Pascal

View 1 Replies View Related

Cascade Updates And Deletes

Feb 27, 2001

It appears that SQL7 does not support options to set cascade updates and deletes in the Relations tab of the property sheet, but it is included in

View 4 Replies View Related

Which Sp Deletes Files In Log Ship

Jan 10, 2005

I have new client we changed the file retention period on for log ship on SQL 2000. Now the files are not being deleted. I looked at all the sp's but could not finde the one that actually deletes the log file that was copied over. I could just rebuild log ship but this is on a prod box, was trying to figure out what and when the files get deleted, and by what sp

View 2 Replies View Related

Blocking Repicated Deletes

Oct 19, 2004

Hi

I keep data in four seperate locations, each with different data retention.
the problem is, i keep the publisher "thin" - only 24 hrs of data) the data in the relevant tables is about 250000 a day) and these deletes are blocked at the subscribers by commenting out the body of the repl proc.
this does however cause a burst in bandwith & locks the subscribers, even though no actual delete takes place.
does anyone know if you can actuaklly NOT REPLICATE delete commands for specified tables at all - so that the message of the delete wont even be SENT to the subscriber?

Des

View 6 Replies View Related

Deletes From Multiple Tables.

Nov 15, 2006

Hello everybody,

We have a Master table in the Db who's PK is referenced in at least 60 tables as FK.
We want to delete all the dependent records in 1 go without using multiple delete statements or any cursor or loop.

Is it possible? Please advice.

Thanks in advance.

Regards,

Ashish

View 14 Replies View Related

Rapid Deletes Cause A Deadlock

May 12, 2004

Hello all.

Please help me shed light on the following problem:

I am deleting from 2 tables in a transaction. I experience a deadlock occasionally when deleting.
Here are the tables:

create table TABLE_1 (
ID integer not null,
TYPE_CD varchar(50) not null,
STATUS_ID integer not null,
NAME varchar(500) not null,
COMMENT varchar(500) null,
CREATED_DATE datetime not null,
CREATED_BY varchar(15) not null,
LASTMOD_DATE datetime null,
LASTMOD_BY varchar(15) null,
constraint PK_TABLE_1 primary key nonclustered (ID)
)

create table TABLE_2 (
TASK_ID integer not null,
TABLE_1_ID integer not null,
COMMENT varchar(500) null,
constraint PK_TABLE_2 primary key nonclustered (TASK_ID, TABLE_1_ID)
)

Table table_2 is an intersection table between table_1 and another lookup table (called task).
Whenever I delete from table_1 I need to delete from table_2 to maintain integrity.
The reason I do not have clustered indexes on the tables is b/c they start out empty (every day a housekeeping task deletes from them. Unfortunately I cannot change this...).

In my (web/jsp) application before I delete, I select from table_1 to get data I need to remember after the delete.
In order to avoid the "select for update problem", I use the "UPDLOCK" hint in the select like this:

select *
from table_1 WITH (UPDLOCK),
table_2 WITH (UPDLOCK)
where table_1.id = table_2.table_1_id "
and table_1.id = ?
and table_2.task_id = ?

I then issue my deletes in the following order:

delete from table_1
where task_id = ?
and table_1_id = ?

and then,

delete from table_1 where id = ?


The application sets the isolation level to READ COMMITTED.
The above 3 sql statements are executed as part of a transaction. Occasionally, when I drive the application, I get a deadlock. This happens intermittently. I have been able to reproduce the problem by attempting to delete records really fast, one after the other from the (web) front end.
When the deadlock occurs these are the locks that are held by the running processes:

Blocked SPID (10):
============

Object Lock Type Mode Status Owner Index Resource
app DB S GRANT Sess TABLE_2
app.dbo.TABLE_1 TAB IX GRANT Xact TABLE_2
app.dbo.TABLE_1 KEY U GRANT Xact PK_TABLE_2 (a400f609034c)
app.dbo.TABLE_1 RID U GRANT Xact TABLE_2 1:157:3
app.dbo.TABLE_1 PAG IU GRANT Xact PK_TABLE_2 1:443
app.dbo.TABLE_1 PAG IU GRANT Xact TABLE_2 1:157
app.dbo.TABLE_2 KEY S WAIT Xact PK_TABLE_2 (6501eda29ac1)
app.dbo.TABLE_2 KEY S GRANT Xact PK_TABLE_2 (ef007b1066ea)
app.dbo.TABLE_2 TAB IS GRANT Xact TABLE_2
app.dbo.TABLE_2 PAG IS GRANT Xact PK_TABLE_2 1:252

Blocking SPID (12):
=============
Object Lock Type Mode Status Owner Index Resource
app DB S GRANT Sess TABLE_2
app.dbo.TABLE_2 KEY X GRANT Xact PK_TABLE_2 (6501eda29ac1)
app.dbo.TABLE_2 RID X GRANT Xact TABLE_2 1:176:3
app.dbo.TABLE_2 TAB IX GRANT Xact TABLE_2
app.dbo.TABLE_2 PAG IX GRANT Xact PK_TABLE_2 1:252
app.dbo.TABLE_2 PAG IX GRANT Xact TABLE_2 1:176

I could not figure out what the problem is. Please help me with this.
Any help will be much appreciated.

Thanks in advance

View 2 Replies View Related

Stop Replication Of Deletes

Jul 23, 2005

Hello all. We are just getting started with replication and I'mwondering if there is a way to not have deletes replicated. I know Icould manually remove the delete trigger from every table but I'mintrested in have this a bit more automatic (like when its creating thesubscription).ThanksStephen

View 1 Replies View Related

Cascade Update/deletes

Apr 28, 2008

Hi guys,

A simple question of good practices: is it better to have a cascade update/delete on a table? Or is it better to do it "by hand" in a SP?


Thanks a lot.

View 1 Replies View Related

Deletes Causing Tran Log To Fill

Aug 2, 2000

I seem to be having a problem on all of my SQL servers. WHen I or a developer attmept to do a delete on a table i get a Log file for database is full. I truncate the log try again and get the same error. IT doesnt seem to matter how much is being deleted or how big the table is. THis is very strange and very frustrating.

Thanks
David

View 5 Replies View Related

WITH CHECK OPTION Is Not Checking For Deletes

May 13, 2006

I was testing how the WITH CHECK OPTION works. I though that it prevents row from dissappearing from the view implementing this option but I found that I was able to DELETE all rows through the view - Why is this???

Please note that I only have the base version of sql server 2000 - No patches applied

Thanks for your help

------------------------------------------- Test

Create table T100 (A int)
GO
Create view VT100 AS (SELECT * FROM T100 WHERE A < 2) with check option
GO
INSERT INTO T100 VALUES (1) -- Part of the view's result
INSERT INTO T100 VALUES (2)
GO
SELECT * FROM VT100 -- SHOWS 1
GO
INSERT INTO VT100 VALUES (-2) -- Works
GO
UPDATE VT100 SET A=5 -- Update fails because of WITH CHECK OPTION - GOOD
GO
INSERT INTO VT100 VALUES (999) -- Inser fails because of WITH CHECK OPTION - GOOD
GO
DELETE FROM VT100 -- DELETES ALL ROWS!!!! ******************* ???


ekareem

View 5 Replies View Related

Tempdb Filling Up - Mass Deletes

Dec 13, 2007

Hi,

I've been having problems with my tempdb filling up, and causing all databases on the server to stop functioning properly. I've been removing alot of data lately (millions of rows), and I think this is the reason why my tempdb log is going thru an unusual load.

Whats the best way to make sure the tempdb doesnt fill up causing me major problems? I had temporarily turned off backups while I was having a new HD put in. Am I right in thinking that when a DB is backed up, the tempdb log is reduced in size? Should maintaining a daily backup solution help keep things under control ?

Thanks very much for any tips!

mike123

View 4 Replies View Related

2005: Forbidding INSERTs And DELETEs

Jul 6, 2006

Please help.I have a table with single row. I need to allow only UPDATEs of thetable, forbid INSERTs and DELETEs. How to achieve it?Thank you for information/RAM/

View 3 Replies View Related

Multiple Deletes With A Stored Procedure

Jul 20, 2005

Just wondering if this is good form:Alter Procedure "mySPName"@UniqueID intASset nocount onset xact_abort offDELETE FROM tblNameOneWHERE(tblNameOne.UniqueID = @UniqueID)DELETE FROM tblNameTwoWHERE(tblNameTwo.UniqueID = @UniqueID)Is it a good idea to run multiple detele statements within one SP?thanks,lq

View 5 Replies View Related

How Do I Stop Replicating Deletes Without A Snaphot?

Oct 25, 2007

I can switch off replication deletes be running the following:

EXEC sp_changearticle @publication = 'PUBNAME', @article = 'ARTICLE1', @property = 'del_cmd', @value = 'NONE'

I believe I can the effectively then remove the delete stored procedure from the replicated database.

And all this wroks no problem. However, when I come to add a new article, "Article2" to my publication it also snapshots and reinitialised the "Article1". How can I prevent the snapshot agent from re-initialising "Article1".

Thanks.

View 4 Replies View Related

How To: Create A Table That Only Allows Inserts And Deletes.

Feb 26, 2008


I need to create a table that only allows records to be inserted or deleted. Once the record has been created it can only be deleted. Is there anyway to configure a table in this manner?

Table Definition

USE [DB_AUTOMATED_PACKAGING_SYSTEM]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TBL_PCL_LENS_DATA](
[SerialNumber] [varchar](50) NOT NULL,
[ProcessedDate] [datetime] NOT NULL,
[Filename] [varchar](50) NOT NULL,
[CartonLabelImage] [image] NOT NULL,
[ExpirationDateLabelImage] [image] NOT NULL,
[LabelSetLabelImage] [image] NOT NULL,
[ReplyCardLabelImage] [image] NOT NULL,
[TextFile] [ntext] NOT NULL,
CONSTRAINT [PK_TBL_PCL_LENS_DATA] PRIMARY KEY CLUSTERED
(
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
SET ANSI_PADDING OFF

View 5 Replies View Related

Deletes On Subscriber In Merge Replication

Oct 20, 2006

Using Merge replication between SQL Server 2000 and SQL Server CE,
is there any way that row deletes could occur on the subscriber without a reinitialize
or explicit delete of row on publisher.

More specifically, if there is a row filter that returns a days worth of data with each
days pull, for example, and the filter looked like
select <columns> from Table where UpdateDate < GETDATE() and UpdateDate >= DATEADD(d,1,GETDATE())
would there be some implicit delete at subscriber each day because data sent changed?

My research indicates this does not happen, but I have a colleague who thinks differently.

View 5 Replies View Related

FYI - Oracle Linked Server - OpenQuery Deletes

Aug 7, 2000

I thought this may be very helpful for those of you using SQL7 linked server to Oracle 7.3.4 Database. After much research and no answers when opening a case with Microsoft I finally figured out how to delete using Openquery. There are a few missing pieces of information that would of been a great help in BOL. The first is, the Microsoft OLE DB for Oracle is not the correct choice for the data provier as one might think. The correct choice is the Oracle Provider for OLE DB. The next important thing is that the Oracle table you are querying MUST have a unique index on at least one column not necessarily the column in your WHERE clause. Thirdly, you get much better performance and use of indexes if you put the WHERE clause inside the OPENQUERY statement. Here is the syntax that I found to work in my application:

DECLARE @ins_id varchar(7)
DECLARE @sqlstring nvarchar(2000)
Select @ins_id = '123456'
Select @ins_id = convert(int, @ins_id)
select @ins_id

select @sqlstring = 'DELETE FROM OPENQUERY(LinkedServerName,"SELECT * FROM OracleTableName WHERE I_ID = '
+ '' + @ins_id + '' + '")'
select @sqlstring

EXEC sp_executesql @sqlstring

*Note...I had an additional data conversion from varchar to integer in my statement.

I hope this information helps any others who are having trouble performing this type of task.

View 2 Replies View Related

SQL2k Profiler - Tracking Inserts And Deletes

Nov 16, 2006

SQL 2K - SP3a - Development BoxThe environment is a piece of garbage ... triggers firing triggers firing triggers. Application is a piece of garbage that a developer want to make better (yea!) I need to put together a profile template to capture inserts and deletes into two tables. Not asking for the fish (but will accept it if necessary). Looking thru BOL, cannot find find an event class to capture insert and / or delete. Looking at TSQL event class gives SQLStmtCompleted event, and maybe I can filter on the two object IDs (object name not available because dbname not available as data column for this event.) Or if exists, but not documented, the event class for insert and delete. Any ideas?

View 1 Replies View Related

Stored Procedure That Deletes Table If There Is Data

Apr 10, 2015

I am trying to create a stored procedure that Deletes Table if there is Data.

Also stored procedure will Insert new data into table.

I have already created table. This is part of my current stored procedure.

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'srd.[PNMACjmerlos].FHLMC_Trials') AND type in (N'U'))
DELETE srd.[PNMACjmerlos].FHLMC_Trials

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved