列名带有某字符串的列 作为搜索条件
列名含有某字符串的列 作为搜索条件
------解决思路----------------------
能想到的只有这个,看看别人有没有更好的方法
------解决思路----------------------
------解决思路----------------------
Product 表结构如下
ProductID BrandName PModel 口径-200-1-koujing 压力等级-300-1-yalidengji
1 ELSTER TRZ2 G65 DN50 PN10bar
2 天信 TRZ2 G70 DN100 PN16bar
3 天信 TRZ2 G65 DN50 PN200bar
现在想要 列名中含有 "koujing"的列,以这一列为搜索条件, 且值为DN50 的记录
结果如下
ProductID BrandName PModel 口径-200-1-koujing 压力等级-300-1-yalidengji
1 ELSTER TRZ2 G65 DN50 PN10bar
3 天信 TRZ2 G65 DN50 PN200bar
------解决思路----------------------
declare @col varchar(100),@sql varchar(max)
select @col=ISNULL(@col+',','')+name from syscolumns where id=OBJECT_ID(N'Product') and name like'%koujing%'
print @col
set @sql='
select '+@col+' from Product
'
exec (@sql)
能想到的只有这个,看看别人有没有更好的方法
------解决思路----------------------
declare @where varchar(1000)='',@sql varchar(max)
--如果koujing唯一则无所谓;如果不唯一,你要考虑多个字段='DN50'的关系是 and 还是 or
select @where=@where+' and '+name +'=''DN50''' from syscolumns where id=OBJECT_ID(N'Product') and name like'%koujing%'
if LEN(@where)>0
set @where=substring(@where,5,1000)
set @sql='select * from Table_1 where '+@where;
print @sql
exec (@sql)
------解决思路----------------------
declare @strSql varchar(1000),
@Rows int,
@Row int = 1,
@TabName varchar(50),
@ColName varchar(50)
declare @ObjTab table (RowNO int identity(1,1) not null,TabName varchar(50),ColName varchar(50))
insert into @ObjTab
select distinct b.Name as TabName, a.Name as ColName from sys.columns a inner join sys.tables b on a.object_id = b.object_id
where a.name like '%koujing%'
set @Rows = @@rowcount
while(@Row <= @Rows)
begin
select @TabName = TabName,@ColName = ColName FROM @ObjTab where RowNO = @Row
set @strSql = ' select * from ' + @TabName + ' where ' + @ColName + ' = ''DN50'' '
print @strSql
exec (@strSql)
set @Row = @Row + 1
end