方法一:declare @columns varchar(max)= ''select @columns = @columns+'['+name+'],' from logs..syscolumns where ID = OBJECT_ID('logs..'+@user+'_tablename')set @columns = replace(@columns,',[objid],','')sqlserver 2014不能用这种方法,因为14中会将name排序之后再进行获取,导致最后replace时,字符串错误,可以用方法二解决此问题。方法二:declare @columns varchar(max)=''select @columns =@columns +','+name From logs..syscolumns where id=object_id('logs..'+@user+'_tablename')and name not in('objid') set @columns = left(@columns ,len(@columns)-1)