Wednesday, 15 February 2012

sql server - SQL query to retrieve 20 columns along with there values from the given table -


edit-1

I need SQL queries that enable me Creates 20 columns before a table and other SQL queries that enable me to get from the 20th column with the values ​​from the same table in the 60th column.

Try with dynamic SQL:

  @columns NVARCHAR (Max ) @params NVARCHAR (Max) announcement declaration = '@columns NVARCHAR (max) output' @sql NVARCHAR (max) declaration = '@columns select = STUFF ((select' ',' '+ [column] info_schema.columns From (TABLE_SCHEMA = '' dbo '' and TABLE_NAME = '' TableName '') and (ordinal_position  gt; = 20 and ordinal_position & lt; = 60)) for XML path (' '' ')), 1,1,' '' '' 'EXEC sp_executesql @sql, @params, @columns Output set @sql =' Select '+ @columns + EXEC (@sql)  

Note that you should ' Take care of ' SQL injection from dbo.TableName, if you will provide a table name or schema with the parameter for this statement.

Edit:

Try this to filter:

  set @sql = 'Select' + @columns + 'from dbo.TableName Where between logtime '2014-10-10 07:17:00' 'and' '2014-10-10 08: 47: 00' ''  

Also check that < Code> log time is in column categories, you can always print to the states after

  EXEC sp_executesql @sql, @params, @columns Output  

Type

  print @columns  

and see that the column is included.


No comments:

Post a Comment