Query to get all the columns in a view I've got this little ditty to concatenate all the columns in a table, for writing scripts quickly:

    create function [dbo].[get_columns] (@table_name varchar(255))
    returns varchar(max)

    as
    begin
    declare @name varchar(255),
    @object_id varchar(255),
    @string varchar(max)


    set @object_id = (select object_id from sys.objects with (nolock) where name = @table_name and type = 'u')

    set @string = (select stuff( (
    select ', ' + name   from sys.columns with (nolock)
    where object_id = @object_id
    and name != 'row_timestamp'
    for xml path ('')),1,2,''))

    return @string
    end


Question: how would you re-write this to get the columns of a view?
Why have one function for tables and a separate function for views? You can handle both just by removing the restrictive filter on type = 'u' (and actually you don't need sys.objects at all). Here is a rewrite with some other minor enhancements:

CREATE FUNCTION dbo.get_columns
(
  @object_name nvarchar(513) 
  -- needs to support Unicode!
  -- should pass schema.object
)
RETURNS nvarchar(max) -- can contain Unicode!
AS
BEGIN -- we don't need any variables
  RETURN 
  (
    SELECT STUFF((SELECT N', ' + name 
      FROM sys.columns WITH (NOLOCK)
      WHERE [object_id] = OBJECT_ID(@object_name)
      AND name &lt;&gt; N'row_timestamp'
      FOR XML PATH(''), TYPE).value(N'.[1]',N'nvarchar(max)'),1,2,'')
  );
END