日期:2014-05-18 浏览次数:20525 次
--try use pubs /*pubs 数据库*/ go declare @sql varchar(8000) set @sql='select ' select @sql=@sql+quotename(Names)+'=['+Names+'] ,' from (SELECT Rtrim(column_name) as Names FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'authors' ) a select @sql=left(@sql, len(@sql)-1) exec(@sql)
------解决方案--------------------
create table T(id int) insert T select 1 insert T select 2 insert T select 3 insert T select 4 insert T select 5 declare @sql varchar(8000) set @sql='select ' select @sql=@sql+quotename(id)+'='''+rtrim(id)+''',' from T select @sql=left(@sql,len(@sql)-1) exec(@sql) drop table T /* 1 2 3 4 5 ---- ---- ---- ---- ---- 1 2 3 4 5 (1 行受影响) */
------解决方案--------------------
use pubs /*pubs 数据库*/ go declare @sql varchar(8000) set @sql='select ' select @sql=@sql+ ''''+quotename(names)+''' as ' + quotename(names) + ',' from (select rtrim(column_name) as names from information_schema.columns where table_name = 'authors' ) a select @sql=left(@sql, len(@sql)-1) exec(@sql)