Friday, March 25, 2011

Randomly Ordering a ResultSet in SQL Server

-- I found this T-SQL function to be quite handy. It returns the records in a random order every time.

SELECT *
FROM myTable
ORDER BY NEWID()

Getting Access to the Response Object from a Regular C# Class (and NOT a code-behind)

When I attempt to call Response.Write() or Response.Redirect() from a genuine C# class (NOT a code-behind), I get an interesting error:

"Response is not available in this context."

However, call it this way and it should work fine:

HttpContext.Current.Response.Write("Hello, World!");

Query to Retrieve Uptime from a SQL Server


SELECT DATEDIFF(D, CrDate, GETDATE()) 'Days On Line'
FROM SysDatabases
WHERE Name = 'TempDb'

Wednesday, March 16, 2011

Getting Column Names from a Table in SQL Server


-- All table info
SELECT *
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'tblName'



-- Column Info
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = 'tblName'

Thursday, March 10, 2011

Html/Meta Object in C#

Don't forget to import System.Web.UI.HtmlControls;

//Waits for 10 seconds and then forwards to Google.com
HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "Refresh";
meta.Content = "10; URL=http://www.google.com";
Page.Header.Controls.Add(meta);

Thursday, March 3, 2011

Moving System Databases in SQL Server 2008

Great article on this. Follow it to the letter from start to finish though, or you could wind up with a non-functioning SQL Server:
Moving System Databases

Change the Default Database File Path


USE [master]
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultData',
REG_SZ, N'S:\DATA'
GO
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'DefaultLog',
REG_SZ, N'L:\SQLLog'
GO


Default Database File Path

Code: 0x80012017 Description: The package path referenced an object that cannot be found

From the command line run this:

REGSVR32 "C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTS.dll"

Make sure that the path in quotations is where the DTS.dll is stored.

Maintenance Plan Failure