marrowyung
asked on
delete date from table
hi,
if I want to delete data in a table of schema:
delete as according to the starttime when startime is older than 3 months ago. what should be the full statement of the where cause ?
if I want to delete data in a table of schema:
CREATE TABLE [dbo].[Events](
[startTime] [datetime] NOT NULL,
[checksum] [int] NOT NULL,
[eventId] [int] NOT NULL,
[eventType] [int] NOT NULL,
[eventClass] [int] NOT NULL,
[eventSubclass] [int] NULL,
[spid] [int] NULL,
[applicationName] [nvarchar](128) NULL,
[hostName] [nvarchar](128) NULL,
[serverName] [nvarchar](128) NULL,
[loginName] [nvarchar](128) NULL,
[success] [int] NULL,
[databaseName] [nvarchar](128) NULL,
[databaseId] [int] NULL,
[dbUserName] [nvarchar](128) NULL,
[objectType] [int] NULL,
[objectName] [nvarchar](512) NULL,
[objectId] [int] NULL,
[permissions] [int] NULL,
[columnPermissions] [int] NULL,
[targetLoginName] [nvarchar](128) NULL,
[targetUserName] [nvarchar](128) NULL,
[roleName] [nvarchar](128) NULL,
[ownerName] [nvarchar](128) NULL,
[targetObject] [nvarchar](512) NULL,
[details] [nvarchar](512) NULL,
[eventCategory] [int] NOT NULL,
[hash] [int] NOT NULL,
[alertLevel] [int] NOT NULL,
[privilegedUser] [int] NOT NULL,
[fileName] [nvarchar](128) NULL,
[linkedServerName] [nvarchar](128) NULL,
[parentName] [nvarchar](128) NULL,
[isSystem] [int] NULL,
[sessionLoginName] [nvarchar](128) NULL,
[providerName] [nvarchar](128) NULL,
[appNameId] [int] NULL,
[hostId] [int] NULL,
[loginId] [int] NULL,
[endTime] [datetime] NULL,
[startSequence] [bigint] NULL,
[endSequence] [bigint] NULL
) ON [PRIMARY]
GO
delete as according to the starttime when startime is older than 3 months ago. what should be the full statement of the where cause ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Marrowyung,
Yes, please check my code sample that I posted - you will see a COMMIT TRAN statement commented out.
The idea is that you run this one COMMIT TRAN line manually on its own separately, but only when you are fully happy that your DELETE has only deleted the required records and has not accidentally deleted too many records.
Please let me know if this explains better for you.
Yes, please check my code sample that I posted - you will see a COMMIT TRAN statement commented out.
The idea is that you run this one COMMIT TRAN line manually on its own separately, but only when you are fully happy that your DELETE has only deleted the required records and has not accidentally deleted too many records.
Please let me know if this explains better for you.
what is the diff between this:
"DELETE FROM dbo.Events
WHERE startTime < DATEADD(MONTH, -3, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))"
and this:
DELETE FROM Events
WHERE startime < GETDATE()-90
I strip the time from the current date in the first code. That ensures it doesn't delete partial days. The second DELETE will delete only part of a day, before whatever the current time is when it happens to run. That can be confusing if you're looking at data later: why do I see records from 1:23pm but not from 2:11pm for that day in the log?
ASKER
"I strip the time from the current date in the first code. That ensures it doesn't delete partial days"
yeah, but it can make t log very full, I prefer to separate it.
"The second DELETE will delete only part of a day, before whatever the current time is when it happens to run. That can be confusing if you're looking at data later: why do I see records from 1:23pm but not from 2:11pm for that day in the log?
I can't see where is second delete
yeah, but it can make t log very full, I prefer to separate it.
"The second DELETE will delete only part of a day, before whatever the current time is when it happens to run. That can be confusing if you're looking at data later: why do I see records from 1:23pm but not from 2:11pm for that day in the log?
I can't see where is second delete
You're doing ninety days at one time, and you claim a few more hours' worth will make the log too big? That doesn't really make any sense.
Good luck with this.
Good luck with this.
ASKER
hi,
I change it to 15 days already sir.
the whole process finish in less than 1 hours.
I change it to 15 days already sir.
the whole process finish in less than 1 hours.
ASKER
tks all.
ASKER
why that approach ? the BEGIN TRAN seems don't have commit tran statement.
ScottPletcher,
"Btw, if you (almost) query this table by startTime, the table should be clustered on startTime first."
why concern on this ?
"DELETE FROM dbo.Events
WHERE startTime < DATEADD(MONTH, -3, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0))"
what is the diff between this and this:
Open in new window
?