create table tb1(id int,name varchar(20),book varchar(20),date datetime)
insert tb1 values(1,'JOKE','SHUXUE','2004-12-12')
create table tb2(s_id varchar(10),id int,s_likeman varchar(20),pricre int)
insert tb2 values('0001',1,'小张',12)
insert tb2 values('0002',1,'小李',13)
declare @sql varchar(8000)
set @sql='select a.book'
select @sql=@sql+',max(case when b.s_likeman='''+s_likeman+''' then b.s_likeman end) s_likeman,
max(case when b.pricre='+cast(pricre as varchar)+' then b.pricre end) pricre'
from (select distinct s_likeman,pricre from tb2) a
exec (@sql+' from tb1 a,tb2 b where a.id=b.id group by a.book')
drop table tb1,tb2
/*
book
s_likeman
pricre
s_likeman
pricre
-------------------- -------------------- ----------- -------------------- -----------
SHUXUE
小李
13
小张
12
*/