Merry Christmas to you all!!!

Tuesday, 26 December 2006 11:12 by admin

Happy Holidays!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:  
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Auto Clear Tables in a Database (T-SQL)

Wednesday, 20 December 2006 02:12 by admin

A colleague in my office asked me if its possible to have a routine that deletes the tables contents using SQL Server. I definitely said yes and redirected him to the DELETE command in T-SQL. I said that the DELETE command would delete all contents of the table if you will not specify a WHERE statement. Thus doing this,

DELETE FROM TableName

would delete all contents of TableName.

But what if you have a 50+ tables in your database? I guess its not practical to write a DELETE statement for each of the table. That's why I've come up with this solution. I'm gonna show the code first and walk you right through it.

DECLARE del_cursor CURSOR FOR 
                 SELECT name FROM dbo.sysobjects
                 WHERE xtype='U'

DECLARE @name nvarchar(128)
DECLARE @sql nvarchar(200)

OPEN del_cursor
FETCH NEXT FROM del_cursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
      SET @sql = 'DELETE FROM dbo.' + @name
      EXEC(@sql)
      FETCH NEXT FROM del_cursor INTO @name
END

CLOSE del_cursor
DEALLOCATE del_cursor

So how does this work? Let me dissect each lines of the code. The first line declares a CURSOR variable. 

DECLARE del_cursor CURSOR FOR 
                 SELECT name FROM dbo.sysobjects
                 WHERE xtype='U'

You can look at a CURSOR variable as a pointer to a table or a resultset. In our case we are pointing to a resultset,

SELECT name FROM dbo.sysobjects
                 WHERE xtype='U'

Before you get confused, the sysobjects table is a system table which is present in all Sql Databases. It's the main storage for objects being used in the database (views, stored procedures, tables, etc...).  What I am actually doing here is navigate the sysobjects table and look for User defined tables. That's what the WHERE xtype='U' means.

So after creating the cursor I've declared 2 variables.

DECLARE @name nvarchar(128)
DECLARE @sql nvarchar(200)

These will be the placeholder for our generated DELETE statement and the tablename. After that, lets open the cursor.

OPEN del_cursor

This action would allocate a name (variable name) for the cursor in the current session. Once opening a cursor don;t forget to close and deallocate it. The next statement initializes the cursor,

FETCH NEXT FROM del_cursor INTO @name

As the statement reads, it would fetch the data from the cursor and store it in the variable @name. We need to do this in order to point our cursor at the very first record in the resultset. So by executing this statement @name now holds the very first name field in our query.  Next, we must traverse the resultset.

WHILE @@FETCH_STATUS = 0

This loop would check if the cursor is still pointing in a valid record. This is to prevent over-looping the resultset. The @@FETCH_STATUS is a global variable which gives back any positive/negative value whenever the cursor is pointing at the beginning or end of resultset.

SET @sql = 'DELETE FROM dbo.' + @name

This statement manually creates the DELETE sql statment to be executed. It uses the @name variable which contains the name of the table.

EXEC(@sql)

This is where the actual execution of the generated DELETE sql to the server.

FETCH NEXT FROM del_cursor INTO @name

Calling this command would move the cursor to the next record in the resultset.

CLOSE del_cursor
DEALLOCATE del_cursor

As I said previously, upon using and opening a cursor you must close and deallocate it. If you don't, an error would generate uopn attempting to open the cursor. Because the cursor is still open in the current connection.

If you want to exclude other tables from being cleared you can check the @name variable if it matches your excluded table. Here;s a sample:

IF @name != 'table1' AND @name != 'table2'
BEGIN
      SET @sql = 'DELETE FROM dbo.' + @name
     EXEC(@sql)
END
FETCH NEXT FROM del_cursor INTO @name

Really hope this simple tutorial helps. :)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:  
Actions:   E-mail | Permalink | Comments (4) | Comment RSSRSS comment feed

Visual Studio 2005 Service Pack 1

Monday, 18 December 2006 22:12 by admin

At last! The long await is over! Service Pack 1 of Visual Studio 2005 has been released! (Actually it has already been released last Friday but I haven't got the time to download it).

This service pack is the first for VS 2005 which was released on November last year. This update resolves some issues that were found on the earlier stage of the software and including some enhancements.

The service pack is quite big (442mb) so it would take time to download depending on your internet bandwidth. There are 3 versions of the service pack, each targetting the different packages available for VS 2005. Here are the links:

Visual Studio® 2005 Team Suite SP1
Visual Studio® 2005 Team Foundation Server SP1
Visual Studio® 2005 Express Editions SP1

As you may have noticed there are no available downloads for Standard and Professional editions and also the other flavors of Team Suite (TS for Database Professionals, Architects, etc...). These products would be patched using the Team Suite service pack.

Upon installation you will notice that your computer seems to freeze. That's ok, initially it took me 15-20 mins just for the installer to initialize. In totality it took me 2hrs and 30mins just to install this service pack. I really was dismayed as it was the same length of time on installing a fresh copy of VS2005.

The reason for this is that this service pack causes a repair on your VS2005 installation coz it fixes several dll's that is crucial to the software. But I have found this very wonderful blog of Heath Stewart which contains many tips on using VS2005 SP1. Here are the topics that helped me most.

Slipstreaming Visual Studio 2005 Service Pack 1
Save Time and Space for VS 2005 SP1 by Disabling the Patch Cache

I have just finished installing the update and noticed a few changes on VS 2005. Here it is:

  • There is a web applicaiton project in the New Projects DIalog. This is a VS2003 approach of creating webpages. In my opinion, this approach is much better coz I myself wants to manage my Web Projects into a System/ Forms-like management.
  • The IDE is much faster. The properties window can now catch up when the user clicks a web control.  And also the switching from Design to Code is also a lot faster now.

Currently these are the only enhancement that I noticed. I will just update this post for future enhancements that would be revealed to me.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:  
Actions:   E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed