你的位置:首页 > 数据库

[数据库]根据目标列的内容使用sql获取列名

    最近需要将房源数据以

declare @tableName varchar(30),@columnStr varchar(200)declare @column varchar(30),@str varchar(300)--要查询的表名set @tableName='article'--要查询的内容set @columnStr='花'declare cur cursor forSelect name from syscolumns Where ID=OBJECT_ID(@tableName) open cur fetch next from cur into @column while (@@FETCH_STATUS=0)  begin   set @str='declare @count int	 set @count=( select top(1) 1 from '+@tableName+' with(nolock) where '+@column +' like ''%'+@columnStr+'%'' )	 if @count=1	 begin	  print '''+@column+char(13) +''' 	 end'	 fetch next from cur into @column	EXEC(@str);  end  close cur  deallocate cur

      期望这段代码能够对有相同需求的人有所帮助。