|
DECLARE @cCount INT
DECLARE @i INT
DECLARE @sql NVARCHAR(255)
SET @sql='select '
DROP TABLE #tt
select COLUMN_NAME INTO #tt from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 't1'
SELECT @cCount=COUNT(COLUMN_NAME) FROM #tt
DECLARE @columnName NVARCHAR(50)
DECLARE cur CURSOR FOR SELECT COLUMN_NAME FROM #tt
OPEN cur
FETCH NEXT FROM cur INTO @columnName
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql=@sql+'sum('+@columnName+'),'
FETCH NEXT FROM cur INTO @columnName
END
SET @sql=SUBSTRING(@sql,1,LEN(@sql)-1)+'from t1'
CLOSE cur
DEALLOCATE cur
PRINT @sql
EXEC sys.sp_executesql @sql |
|