Select entire column using function

Is it possible that I am applying the function to all columns in SELECT. For instance,

SELECT LEN(t.*) FROM Table t; 

The problem is that the table is dynamic with a dynamic number of columns, and I need to apply the function to the evry column.

+4
source share
1 answer

No, you need dynamic sql;

 declare @table varchar(256) = 'the_table' declare @sql nvarchar(4000) = '' select @sql += case @sql when '' then '' else ',' end + ' func(' + quotename(column_name) + ') as ' + quotename(column_name) from information_schema.columns where table_name = @table set @sql = 'select' + @sql + ' from ' + @table exec(@sql) 

which produces and performs

 select func([fld1]) as [fld1], func([fld2]) as [fld2] ... from the_table 
+3
source

Source: https://habr.com/ru/post/1381277/


All Articles