--This is another variation used to document a large database for conversion (Edited to --remove static columns)
SELECT o.Name as Table_Name, c.Name as Field_Name, t.Name as Data_Type, t.length as Length_Size, t.prec as Precision_FROM syscolumns cINNER JOIN sysobjects o ON o.id = c.idLEFT JOIN systypes t on t.xtype = c.xtypeWHERE o.type = 'U'ORDER BY o.Name, c.Name
--In the left join, c.type is replaced by c.xtype to get varchar types
SELECT column_name, data_type, character_maximum_length, table_name,ordinal_position, is_nullableFROM information_schema.COLUMNS WHERE table_name LIKE 'YOUR_TABLE_NAME'ORDER BY ordinal_position
SELECTT.TABLE_NAME AS 'TABLE NAME',C.COLUMN_NAME AS 'COLUMN NAME'FROM INFORMATION_SCHEMA.TABLES TINNER JOIN INFORMATION_SCHEMA.COLUMNS C ON T.TABLE_NAME=C.TABLE_NAMEWHERE T.TABLE_TYPE='BASE TABLE'AND T.TABLE_NAME LIKE 'Your Table Name'
DROP TABLE IF EXISTS test;CREATE TABLE test (col001 INTEGER, col002 INTEGER, col003 INTEGER, col004 INTEGER, col005 INTEGER, col006 INTEGER, col007 INTEGER, col008 INTEGER, col009 INTEGER, col010 INTEGER);INSERT INTO test(col001) VALUES(1);INSERT INTO test(col002) VALUES(1);INSERT INTO test(col005) VALUES(1);INSERT INTO test(col009) VALUES(1);INSERT INTO test VALUES (NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
SELECTCASE ROW_NUMBER() OVER(ORDER BY ordinal_position)WHEN 1 THEN'SELECT'+CHAR(10)+' *'+CHAR(10)+'FROM test'+CHAR(10)+'WHERE 'ELSE' OR 'END+ column_name +' IS NOT NULL'+ CASE ROW_NUMBER() OVER(ORDER BY ordinal_position DESC)WHEN 1 THENCHAR(10)+';'ELSE''ENDFROM information_schema.columnsWHERE table_schema='dbo'AND table_name = 'test'ORDER BYordinal_position;
-- the whole scenario. Works for 10 , will work for 100, too:
-- out ------------------------------------------------- out SELECT-- out *-- out FROM test-- out WHERE col001 IS NOT NULL-- out OR col002 IS NOT NULL-- out OR col003 IS NOT NULL-- out OR col004 IS NOT NULL-- out OR col005 IS NOT NULL-- out OR col006 IS NOT NULL-- out OR col007 IS NOT NULL-- out OR col008 IS NOT NULL-- out OR col009 IS NOT NULL-- out OR col010 IS NOT NULL-- out ;