I need to control the size of table in database in Ms Sql server 2005.
My objective is to see if the table size grows beyond a certain limit like 300mb or so. If it grows more then this then fire a trigger and remove data from table in which last month entries should be removed to add more fresh data.
Now is trigger going to solve this problem.
Please provide me the code format required for this.
Or any alternative with some good link if possible.
Thanks
Vijendra
1 2327 iburyak 1,017
Recognized Expert Top Contributor
See if this one would work. I had this function in Server 2000. Didn't have time to test if it works on SQL 2005. Let me know how it works, please.
Step 1.
Create function that counts approximate data size in KB. - create function spaceused (@ObjectName varchar(200), @RowNumber int)
-
RETURNS int
-
AS
-
begin
-
declare @id int, @pages int, @res int
-
set @id = object_id(@ObjectName)
-
-
Declare @spt_space table
-
(
-
rows int null,
-
reserved dec(15) null,
-
data dec(15) null,
-
indexp dec(15) null,
-
unused dec(15) null
-
)
-
-
-
insert into @spt_space (reserved)
-
select sum(reserved)
-
from sysindexes
-
where indid in (0, 1, 255)
-
and id = @id
-
-
/*
-
** data: sum(dpages) where indid < 2
-
** + sum(used) where indid = 255 (text)
-
*/
-
select @pages = sum(dpages)
-
from sysindexes
-
where indid < 2
-
and id = @id
-
-
select @pages = @pages + isnull(sum(used), 0)
-
from sysindexes
-
where indid = 255
-
and id = @id
-
update @spt_space
-
set data = @pages
-
-
-
/* index: sum(used) where indid in (0, 1, 255) - data */
-
update @spt_space
-
set indexp = (select sum(used)
-
from sysindexes
-
where indid in (0, 1, 255)
-
and id = @id)
-
- data
-
-
/* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
-
update @spt_space
-
set unused = reserved
-
- (select sum(used)
-
from sysindexes
-
where indid in (0, 1, 255)
-
and id = @id)
-
-
update @spt_space
-
set rows = i.rows
-
from sysindexes i
-
where i.indid < 2
-
and i.id = @id
-
-
IF @RowNumber = 0
-
-
select
-
@res =((data * 8192 / 1024.) + (indexp * 8192 / 1024.))
-
from @spt_space
-
ELSE
-
-
select @res = ((data * 8192 / 1024.) + (indexp * 8192 / 1024.)) * @RowNumber / rows
-
from @spt_space
-
-
-
-
RETURN @res
-
end
Step 2
Write a trigger. Replace table_name, date_column with real name. - CREATE TRIGGER tI_table_name ON table_name FOR INSERT
-
AS
-
-
IF dbo.spaceused ('table_name', 0) > 307200
-
-
Delete from table_name where date_column < dateadd(m, -1, getdate())
Suggestion: TEST EVERYTHING FIRST!!!
NOTE: I wrote function in Step1 to count how much data I about to load in advance.
If you replace 0 with a number of rows you about to add to a table you will have space it will take for this particular table counting data + index size.
Parameter 0 will return current size of a table.
Good Luck.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Martin |
last post by:
Hello,
I'm new with triggers and I can not find any good example on how to
do the following:
I have two tables WO and PM with the following fields:
WO.WONUM, VARCHAR(10)
WO.PMNUM, VARCHAR(10)
WO.PROBLEMCODE, VARCHAR(8)
|
by: Mary |
last post by:
We are developing a DB2 V7 z/OS application which uses a "trigger"
table containing numerous triggers - each of which is activated by an
UPDATE to a different column of this "trigger" table. When the
triggers are fired, various other operations are performed on other
tables in the database. The triggers are not created on these other...
|
by: William of Ockham |
last post by:
Hi,
I was asked to recreate a new clean database for our developers because
the current one they use is not entirely up to date. So I created a new
database and I run into the followin strange problem. First some facts:
System: DB2 V8.1 Fixpack5 Redhat Linux 8.0 dual processor
Database A is the current database and all DDL I currently...
|
by: JohnO |
last post by:
Hi Folks,
I have an update trigger that fails (it inserts an audit table record)
in some circumstances. This is causing the triggering transaction to
fail and roll back. Is there any way to prevent this? If the trigger
fails I still want the triggering transaction to continue.
Cheers,
JohnO
|
by: Jchick |
last post by:
Im a newbie with a sql table in a MSDE database that contains fields of
CustName, BrokerName, Type, Status.
I am trying to write a trigger that does this:
1. When a new record in a table is added, I would like the selected
fields to be copied to another table (call it Master).
2. If that Master table already contains a record where the...
| |
by: pvpkumar |
last post by:
Getting a trigger problem in SQL. As the table grows the trigger stops working. Upon reinstallation of SQL the trigger starts working.
The problem was encountered for the following database/table details
1. Database size 2.25 GB
Table size over 2 million records.
2. Database size 250 MB
Table size...
|
by: Ots |
last post by:
I'm using SQL 2000, which is integrated with a VB.NET 2003 app.
I have an Audit trigger that logs changes to tables. I want to apply
this trigger to many different tables. It's the same trigger, with the
exception of the table name. I could manually change the table name in
the trigger and create it, over and over, but I'd like to automate...
|
by: jan.marien |
last post by:
we have a table with jobs and a table with job_history information.
Users can define jobs and let them run every X minutes/hours , like a
cronjob.
The jobs table has the following trigger:
CREATE TRIGGER JOBS_AFTER_DELETE
AFTER DELETE ON JOBS
REFERENCING OLD AS o
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
|
by: RogBaker |
last post by:
I haven't gotten a response yet, so I moved this from another group. I
have been working on this for 2 days so if anyone has any ideas, I
would be grateful.
I have a 3rd party program that creates and populates tables in my
SQL
Server 2005 database.
The program fails on the inserts on "tblB" because the field it
creates is too small for...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |