我有一个包含列名称的表a1,a2...,b1.b2...
.
我如何选择所有具有列名称的列,例如a%
?
这将为您提供列表
select * from information_schema.columns
where table_name='table1' and column_name like 'a%'
如果你想用它来构造查询,你可以这样做:
declare @sql nvarchar(max)
set @sql = 'select '
select @sql = @sql + '[' + column_name +'],'
from information_schema.columns
where table_name='table1' and column_name like 'a%'
set @sql = left(@sql,len(@sql)-1) -- remove trailing comma
set @sql = @sql + ' from table1'
exec sp_executesql @sql
请注意,以上内容是针对 SQL Server 编写的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)