SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE function [dbo].[Util_ColumnString] (@TableName varchar(MAX)) returns varchar(MAX) as BEGIN /* Tired of spelling out dozens of columns in a select query^ This function returns a string suitable for use as a column list for a specified table. The parameter is the table name to get a column list for. */ declare @ColumnName varchar(MAX) declare @ColumnString varchar(MAX) set @ColumnString = '' declare #TableInfo Cursor Local forward_Only Static for SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @TableName open #TableInfo fetch next from #TableInfo into @ColumnName while @@Fetch_Status = 0 begin set @ColumnString = @ColumnString + '['+ltrim(rtrim(@Columnname))+']'+',' fetch next from #TableInfo into @ColumnName end close #TableInfo deallocate #TableInfo if len(@columnString)>0 set @ColumnString = substring(@ColumnString,1,len(@ColumnString)-1) RETURN @ColumnString END