日期:2014-05-18 浏览次数:20542 次
得到表中除Col1、Col2的所有列 例如:userno_fm、userno_to create table test( num int identity(1,1), userno_fm varchar(10), userno_to varchar(10), username varchar(10)) select * from test declare @sql varchar(8000) select @sql='' select @sql=@sql+','+[name] from (select [name] from syscolumns where object_id(N'[test]')=[id] and [name] not in ('userno_fm','userno_to')) A set @sql='select '+stuff(@sql,1,1,'')+' from [test]' --print @sql exec (@sql) drop table test
------解决方案--------------------
动态构造..从syscolumns下入手
------解决方案--------------------
动态sql declare @sql varchar(8000) select @sql='select 'A.*'+select (select ','+cast(column_name as varchar) as [text()] from information_schema.columns where table_name='B' and column_name<>'字段3' for xml path(''))+' from A,B where A.ID = B.ID ' exec (@sql)
------解决方案--------------------
的确够懒的。
告诉你一个办法,生成查询语句后删除之。