--从系统表中取字段生成动态语句 --table_name:你要查找的表名 --not in('col1','col2'...):不要的列名declare @sql varchar(8000) set @sql='' select @sql=@sql+','+[name] from (select [name] from syscolumns where object_id('table_name')=[id] and [name] not in ('col1','col2')) Aset @sql='select '+stuff(@sql,1,1,'')+' from table_name'exec (@sql)
得到表中除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 testdeclare @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
--table_name:你要查找的表名
--not in('col1','col2'...):不要的列名declare @sql varchar(8000)
set @sql=''
select @sql=@sql+','+[name] from (select [name] from syscolumns where object_id('table_name')=[id] and [name] not in ('col1','col2')) Aset @sql='select '+stuff(@sql,1,1,'')+' from table_name'exec (@sql)
create table test(
num int identity(1,1),
userno_fm varchar(10),
userno_to varchar(10),
username varchar(10))
select * from testdeclare @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