Friday, February 20, 2009

SQL Server: how to delete non-system databases from a cursor

DECLARE @name nvarchar(200)
DECLARE @stmt nvarchar(200)
DECLARE dbCursor CURSOR FOR SELECT D.name FROM sys.databases D where owner_sid <> 0x01
OPEN dbCursor
FETCH NEXT FROM dbCursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
--next IF is not needed, as cursor specifies that owner_sid <> 0x01 already
--IF @name <> 'master' and @name <> 'model' and @name <> 'msdb' and @name <> 'tempdb'
--BEGIN
PRINT 'Name = ' + @name
SET @stmt = 'DROP DATABASE "' + @name + '"'
PRINT 'stmt = ' + @stmt
EXECUTE sp_executesql @stmt
--END
FETCH NEXT FROM dbCursor INTO @name
END
CLOSE dbCursor
DEALLOCATE dbCursor



Notice couple of tips (at least for someone that doesn't write T-SQL every morning ☺):

- SQL syntax doesn't allow some statements to be run from inside a cursor, so you have to call sp_executesql store proc.
- since '-' is a reserved operator, you have to enclose the DB on quotes to successfully operate on a DB whose name contains '-' or other reserved operator.
- owner_sid <> 0x01 filters out system DB's (preferred over the commented IF)

Friday, January 30, 2009

SQL Server: how to get the sizes of the columns of a table

select C.name, C.max_length from sys.columns C
inner join
sys.tables T
on C.object_id = T.object_id and T.name = '<table name>'

SQL Server: how to see Log Transaction status

select name, log_reuse_wait, log_reuse_wait_desc from sys.databases

SQL Server: how to get the sizes of all tables

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

Tuesday, November 18, 2008

What's installed in a Windows box?

See here for downloading and use msiinv.exe.

Wednesday, November 12, 2008

IIS7: COM 0x800703E3 error

Problem: when requesting static files against IIS7, following error was reported to event viewer

0x800703E3
at System.Web.Hosting.IIS7WorkerRequest.FlushCore(Boolean keepConnected, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32[] bodyFragmentTypes)
at System.Web.Hosting.IIS7WorkerRequest.FlushCachedResponse(Boolean isFinal)
at System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders)
at System.Web.HttpRuntime.FinishRequestNotification(IIS7WorkerRequest wr, HttpContext context, RequestNotificationStatus& status)

Solution: known issue in IIS7, fixed by Microsoft .NET Framework 3.5 Service Pack 1