DECLARE @T TABLE (columnName varchar(100) NOT NULL PRIMARY KEY);
INSERT INTO @T
SELECT columnName from QuanGuo_Credit..shixin_quanguo where isnull(columnName,'')!='' group by columnName;
DECLARE @SQL varchar(max)=N'';
SET @SQL=STUFF((SELECT N','+QUOTENAME(columnName) FROM @T
FOR XML PATH('')),1,1,N'');
--PRINT @SQL;
SET @SQL=N'select * from (select columnName, columnValue,oid from QuanGuo_Credit..shixin_quanguo where oid=1586141) as ord
pivot(max(columnValue) for columnName in('+@SQL+N'))as p';
--PRINT @SQL;
--EXEC (@SQL);
-------------------------------------------
declare @sql varchar(8000) set @sql = 'select oid,record_id'
select @sql = @sql + ',max(case columnName when ''' + columnName + ''' then columnValue else null end) [' + columnName +
']'from (
select distinct(b.columnName) from Table_Html a,Table_Columns b where a.table_name='深圳国税局-税务登记证失效公告'
and a.ID=b.tableID )as t
set @sql = @sql + 'from (
select columnName ,columnID,columnValue, oid=b.tableid,record_id from Table_ColumnValue A
left join Table_Columns B on A.columnID=B.ID where B.tableid in (select id from table_html where table_name=' +'''深圳国税
局-税务登记证失效公告'''+')
) as d group by oid,record_id order by oid'
print(@sql)
exec(@sql)
---------------------------------------------------
select oid ,max(case columnName when '案号' then columnValue else null end) [案号],max(case columnName when '被执行人的履行
情况' then columnValue else null end) [被执行人的履行情况],max(case columnName when '地域名称' then columnValue else null
end) [地域名称],max(case columnName when '发布时间' then columnValue else null end) [发布时间],max(case columnName when '立
案时间' then columnValue else null end) [立案时间],max(case columnName when '年龄' then columnValue else null end) [年
龄],max(case columnName when '企业法人/负责人姓名' then columnValue else null end) [企业法人/负责人姓名],max(case
columnName when '身份证号' then columnValue else null end) [身份证号],max(case columnName when '失信被执行人具体情形' then
columnValue else null end) [失信被执行人具体情形],max(case columnName when '未履行部分' then columnValue else null end) [未
履行部分],max(case columnName when '性别' then columnValue else null end) [性别],max(case columnName when '已履行部分' then
columnValue else null end) [已履行部分],max(case columnName when '执行法院' then columnValue else null end) [执行法院],max
(case columnName when '执行依据文号' then columnValue else null end) [执行依据文号],max(case columnName when '作出执行依据
单位' then columnValue else null end) [作出执行依据单位]
from (
select columnName, columnID, columnValue,oid=B.tableID --ROW_NUMBER()OVER(PARTITION BY columnID ORDER BY A.id)oid
from Table_ColumnValue A
left join Table_Columns B on A.columnID=B.ID
where B.tableID in(select top 5 id from Table_Html)
) as a group by oid