Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
JMMILLS
Starting Member
2 Posts |
Posted - 2009-09-01 : 12:47:35
|
I am trying re-learn SQL back on my home and hear is my issue. I have a table called tblTrips whose ID is the primary key of that table. It is also the FK of tbltripsscheduled, table. It is also the FK of tbltripsscheduled, tbltripstemplate and tbltripsactual. I want to be able to delete all of the data from 06/01/2008 and backwards. I was thiking I needed to do something like this:DELETE FROM tblTripsFROM [ID].tblTrips AS ID INNER JOIN TripID.tblTripsactual AS TripID ON ID.ID = TripID.TRIPIDWHERE TRIPID.TRIPDATE > '20080601'I then realized that I can't delete anything from trips since it's primary key is also the foreign key. So then I just deleted the tbltripsscheduled, tbltripstemplate and tbltripsactual tables without realizing that I still need that data to delete and <> does not equal doesn't work for deletingI need assistance making one scriot that will loop the three tables with the FK and the PK Table into deleting all the data at once. I am scratching my head here. Please help! |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-02 : 06:30:34
|
what you need is to get the id values that you need to delete onto a temporary table. likeSELECT ID INTO #tempFROM tblTripsWHERE <your condition>then join to this table on id to delete from others likeDELETE ts FROM tbltripsscheduled tsJOIN #temp ton t.id=ts.TripID...and finally from main table tbltrips |
|
|
JMMILLS
Starting Member
2 Posts |
Posted - 2009-09-02 : 10:17:45
|
I wanted to make this a stored procedure:CREATE PROC dbo.deleteData (@CutOffDate datetime)ASBEGIN SET NOCOUNT ON IF @CutOffDate IS NULL BEGIN SET @CutOffDate = DATEADD(mm, -12, CURRENT_TIMESTAMP) END ELSE BEGIN IF @CutOffDate > DATEADD(mm, -12, CURRENT_TIMESTAMP) BEGIN RAISERROR ('Cannot delete date from last tweleve months', 16, 1) RETURN -1 ENDENDBEGIN TRAN SELECT ID INTO #ARCHFROM tblTripsWHERE effectiveenddate < @cutoffdateDELETE TripID from tblTripsScheduled.TripIDJoin #ARCH.IDON tblTripsScheduled.TripID = tblTrips.IDDELETE TripID from tblTripsTemplate.TripIDJoin #ARCH.IDON tblTripsTemplate.TripID = tblTrips.IDDELETE TripID from tblTripsActual.TripIDJoin #ARCH.IDON tblTripsActual.TripID= tblTrips.IDDELETE ID from tblTripsJoin #ARCH.IDON tblTrips.ID = tblTrips.ID IF @@ERROR <> 0 BEGIN ROLLBACK TRAN RAISERROR ('Error occured while deleting data from dbo.tbltripsscheduled', 16, 1) RETURN -1 END COMMITsg 208, Level 16, State 1, Procedure OOO, Line 27Invalid object name 'TripID'.Msg 208, Level 16, State 1, Procedure OOO, Line 27Invalid object name 'tblTripsScheduled.TripID'.Msg 208, Level 16, State 1, Procedure OOO, Line 27Invalid object name '#ARCH.ID'.Msg 266, Level 16, State 2, Procedure OOO, Line 49Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 0, current count = 1.ID in tbltrips is the PK and TripID is the FK in all the other tables |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-02 : 10:21:41
|
[code]CREATE PROC dbo.deleteData (@CutOffDate datetime)ASBEGINSET NOCOUNT ONIF @CutOffDate IS NULL BEGINSET @CutOffDate = DATEADD(mm, -12, CURRENT_TIMESTAMP)ENDELSEBEGINIF @CutOffDate > DATEADD(mm, -12, CURRENT_TIMESTAMP)BEGINRAISERROR ('Cannot delete date from last tweleve months', 16, 1)RETURN -1ENDENDBEGIN TRAN datadeleteSELECT ID INTO #ARCHFROM tblTripsWHERE effectiveenddate < @cutoffdateDELETE TripID from tblTripsScheduledJoin #ARCH aON a.ID=tblTripsScheduled.TripID DELETE TripID from tblTripsTemplateJoin #ARCH aON a.ID=tblTripsTemplate.TripID DELETE TripID from tblTripsActualJoin #ARCH aON a.ID=tblTripsActual.TripID DELETE ID from tblTripsJoin #ARCH aON a.ID=tblTrips.TripID IF @@ERROR <> 0BEGINROLLBACK TRAN datadeleteRAISERROR ('Error occured while deleting data from dbo.tbltripsscheduled', 16, 1)RETURN -1ENDCOMMIT TRAN datadelete[/code] |
|
|
|
|
|
|
|