Web Developer (RSS)

Topics related to web developers!

Blog moved to WordPress and new domain...

I've moved my blog to my own domain and onto a WordPress engine, CS + SPAM was driving me nuts!

Catchup posts: Oh yes, I've been in Japan for a couple of months, first 2 weeks of photos are uploaded on Flickr, I still havent had a chance to upload the thousands after.

Whats more, I've actually written some information about me so you can finally find out my sad sad way of life. See About Thushan Fernando.

SOFTWARE: Visual Studio .NET 2008 and .NET 3.5 Released!

Quick post before our Games Programming exam in a few hours, Scott Guthrie's blog has just announced the launch of Visual Studio  .NET 2008 and .NET Framework 3.5...

Yayyy! All the details are on his blog. Download away!

SOFTWARE: Visual Studio 2008 RTM Next week!

Well what an exciting few weeks/month this has been. We've had:
Just when you thought things couldnt get better and as predicted in an earlier post (well I cheated a bit!) now Microsoft will finally RTM Visual Studio 2008 early next week. MSDN Subscribers can download it from the "Top Subscriber Downloads" list according to the MSDN Subscriptions Blog.

SQL: Clean a SqlServer Database of tables and Stored Procedures Script

Here's a quick script I wrote to assist in cleaning up a SQL Server database of all tables and Stored Procedures.

WARNING:
THIS *WILL* REMOVE everything in the currently selected database, so USE WITH *EXTREME* CAUTION for heaven's sake! Dont blame me if you blow your company's database without a backup!


Replace [DATABASE_NAME] with whichever DB you would like to clean. This takes on from my previously posted tip about using the undocumented sp_MSforeachtable stored procedure to drop tables post. This is only tested in SQL Server 2005 & 2008, should work in 2000.
------------------------------------------------------
-- Database Cleaner-up-er for SQL Server 2005
------------------------------------------------------
--  Version:	1.0.0
--  Date:	15/09/2007
--  Author:	Thushan Fernando (thushan [ at ] wsoftware [dot ] biz)
--  URL:	http://blogs.developerfusion.co.uk/blogs/thushan/archive/2007/09/15/3320.aspx	
--	
--  Cleans a database removing all tables and stored
--  procedures.
--  
--  THIS WILL DELETE / DESTROY ALL TABLES AND STORED
--  PROCEDURES. YOU HAVE BEEN WARNED HOMEY!!!
-------------------------------------------------------

USE [DATABASE_NAME]

GO
-- INIT
SET NOCOUNT ON

-- VARIABLES
DECLARE @COMMAND varchar(255)	-- Command to execute
DECLARE @COUNTER int			-- Counter used to iterate
DECLARE @spName varchar(255)	-- Stored procedure name

-- DELETE ALL THE TABLES
-- First disable any constraints on that table
EXEC sp_MSforeachtable @command1 = "ALTER TABLE ? NOCHECK CONSTRAINT ALL"

-- Drop the table
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"

-- CREATE A TEMPORARY TABLE
CREATE TABLE #StoredProcedures
(ID int IDENTITY (1,1),
 ProcName varchar(128) NOT NULL)

-- INSERT OUR LIST OF STORED PROCEDURES
INSERT INTO #StoredProcedures (ProcName)
	SELECT ROUTINE_NAME 
		FROM INFORMATION_SCHEMA.ROUTINES
	WHERE ROUTINE_NAME NOT LIKE 'dt_%' AND ROUTINE_TYPE = 'PROCEDURE'

-- Get the number of StoredProc's in the database
SELECT @COUNTER = MAX(ID) FROM #StoredProcedures

-- Iterate through the items
WHILE @COUNTER > 0
	BEGIN
		-- Get the name of the Object
		SELECT @spName = ProcName	FROM #StoredProcedures WHERE ID = @COUNTER

		-- Make the command  to execute
		SELECT @COMMAND = 'DROP PROCEDURE ' + @spName
		
		-- Execute the command
		EXEC(@COMMAND)

		-- Reduce the counter
		SET @COUNTER = @COUNTER - 1
	END

-- Cleanup the temporary table
DROP TABLE #StoredProcedures

SET NOCOUNT OFF
GO
There's probably a better way of doing this - asside from DROP DATABASE [DATABASE_NAME] (har-har!), but IWFM!!!

HOWTO: Drop all tables in a SqlServer database automajically with hidden stored proc sp_MSforeachtable!!!

Sometimes you just want to start fresh, clean slate, forget the mistakes of the past and just start out brand new. By this I mean for your SqlServer database - not real life or anything... although... How to do that when you dont have Enterprise Manager or SQL Server Manager  handy?

Easy! By using the special hidden MSforeachtable stored proc to retrieve the tables in the database and execute a command for each entry.

*BUT* BE WARNED!!! THIS IS NOT REVERSABLE. SO BE CAREFUL!
 -- EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
That will remove all the Tables in the database. You may need to run it a couple of times if there are any foreign Key's blocking a table from being removed. You can also use the MSforeachtable to iterate through tables and re-index or check how much space they are taking:
-- EXEC sp_MSforeachtable @command1 = "sp_spaceused '?'"
-- EXEC sp_MSforeachtable @command1 = "sp_MStablespace '?'"
Funky dory aye?