sql server - Convert text into db object and field identifier -
given select statement lists tables , columns, how convert table , column names identifiers can used in seperate select statement?
in other words, if have string @table = 'person' , @column = 'name', want this:
select datalength(max(t.@column)) longest, datalength(min(t.@column)) shortest @table t;
of course not accomplish want. want know length of longest , shortest string stored in column. if wanted information single column, there no need use these stringified-variables. want every (varchar) column across database. life short create every sql statement accomplish this. why want parametric mechanism specifying table , column. how fix achieve goal?
of course going wrong. perhaps should address each column index, , convert column name when output needed. thoughts?
declare @sql varchar(999) declare @col varchar(50) declare @table varchar(50) set @col = <colname> set @table = <tablename> set @sql = ' select datalength(max(t.@column)) longest, datalength(min(t.@column)) shortest @table t' set @sql = replace(replace(@sql, '@column', @col), '@table', @table) exec(@sql)
Comments
Post a Comment