Wednesday, December 14, 2005 - Posts

New Books Online Available

Keeping to their promise, Microsoft plans to release a version of Books Online every quarter for SQL Server 2005. A new version of the SQL server 2005 Books Online is now available only a month after the RTM release at:

The download has new tutorials, instructions and descriptions of common tasks. It's worth the download for sure but downloader beware: the download is 113 MB!

--Brian Knight

Three methods for dynamic queries

There are three main ways to do dynamic queries inside of SQL Server. My personal favorite method is option one below since it's a single line of code. Option three helps with caching of parameters though. Needless to say, dynamic queries should be avoided whenever possible :).

DECLARE @ColumnName sysname
@TableName sysname
@strSQL varchar(255)
DECLARE @unicodestrSQL nvarchar(255)

SET @TableName = 'Employees'
SET @ColumnName = 'LastName'

--Method one

EXEC ('select max(datalength('+@ColumnName+')) from ' + @TableName);

--Method two

SET @strSQL = 'select max(datalength('+@ColumnName+')) from ' + @TableName
EXEC (@strSQL)

--Method three

SET @unicodestrSQL = N'select max(datalength('+@ColumnName+')) from ' + @TableName
EXEC sp_executesql @unicodestrSQL;

-- Brian Knight