473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trigger to control table size in database

1 New Member
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
Jan 9 '08 #1
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.

Expand|Select|Wrap|Line Numbers
  1. create function spaceused (@ObjectName varchar(200), @RowNumber int)  
  2. RETURNS int   
  3. AS  
  4. begin  
  5. declare @id int, @pages int, @res int  
  6. set @id = object_id(@ObjectName)  
  7.  
  8. Declare @spt_space table    
  9. (    
  10.  rows  int null,    
  11.  reserved dec(15) null,    
  12.  data  dec(15) null,    
  13.  indexp  dec(15) null,    
  14.  unused  dec(15) null    
  15. )    
  16.  
  17.  
  18.  insert into @spt_space (reserved)    
  19.   select sum(reserved)    
  20.    from sysindexes    
  21.     where indid in (0, 1, 255)    
  22.      and id = @id    
  23.  
  24.  /*    
  25.  ** data: sum(dpages) where indid < 2    
  26.  ** + sum(used) where indid = 255 (text)    
  27.  */    
  28.  select @pages = sum(dpages)    
  29.    from sysindexes    
  30.     where indid < 2    
  31.      and id = @id    
  32.  
  33.  select @pages = @pages + isnull(sum(used), 0)    
  34.   from sysindexes    
  35.    where indid = 255    
  36.     and id = @id    
  37.  update @spt_space    
  38.   set data = @pages    
  39.  
  40.  
  41.  /* index: sum(used) where indid in (0, 1, 255) - data */    
  42.  update @spt_space    
  43.   set indexp = (select sum(used)    
  44.     from sysindexes    
  45.      where indid in (0, 1, 255)    
  46.       and id = @id)    
  47.        - data    
  48.  
  49.  /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */    
  50.  update @spt_space    
  51.   set unused = reserved    
  52.     - (select sum(used)    
  53.      from sysindexes    
  54.       where indid in (0, 1, 255)    
  55.        and id = @id)    
  56.  
  57.  update @spt_space    
  58.   set rows = i.rows    
  59.    from sysindexes i    
  60.     where i.indid < 2    
  61.      and i.id = @id    
  62.  
  63. IF @RowNumber = 0   
  64.  
  65.          select   
  66.          @res =((data * 8192 / 1024.) + (indexp * 8192 / 1024.))  
  67.          from @spt_space  
  68. ELSE  
  69.  
  70.         select  @res = ((data * 8192 / 1024.) + (indexp * 8192 / 1024.)) * @RowNumber / rows         
  71.         from @spt_space  
  72.  
  73.  
  74.  
  75. RETURN @res  
  76. end  
Step 2
Write a trigger. Replace table_name, date_column with real name.

Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER tI_table_name ON table_name FOR INSERT 
  2. AS  
  3.  
  4. IF dbo.spaceused ('table_name', 0) > 307200
  5.  
  6. 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.
Jan 9 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3449
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)
6
7126
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...
5
4515
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...
6
9219
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
6
3610
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...
3
1803
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...
9
9299
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...
1
3344
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
7
3052
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...
0
7438
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...
0
7707
Oralloy
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. ...
0
7951
jinu1996
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...
1
7466
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...
0
7803
tracyyun
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...
1
5362
isladogs
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...
0
5082
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...
0
3495
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...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.