Custom Search
My Zimbio

Friday, January 7, 2011

Table with a lot of columns - easy parsing to do a select statement

--USE FOR XML PATH - MUST BE SQL2005 OR HIGHER.


SELECT 'SELECT ' + '''' + so.Name +''', as TblName ' , (SELECT sc.Name + ', ' AS 'data()' FROM dbo.SysColumns sc WHERE so.ID = sc.ID FOR XML Path('')) AS PARSESTRING FROM Sysobjects so where so.xtype = 'U'
and so.name = 'mytablewithlotsofcolumns'

Then just finish it off with adding the from in the parser string.

Comment on other methods that you have used.