表A(价格表)
  F_Name      F_Price1        F_price2
   普通         10                5
   金卡          9                4
   银卡          8                3表B(规格表)
  F_ID    F_specname      F_Spec      
  1        盒子            规格1
  2        盒子            规格2
  3        盒子            规格3
按以上两表,动态生成列,结果如下表F_sprice1|10元  F_sprice1|9 元   F_sprice1|8元 F_sprice2| 5元 F_sprice2|4元 F_sprice2|3元 盒子|规格1 盒子|规格2 盒子|规格3

解决方案 »

  1.   

    create table A(F_Name nvarchar(10), F_Price1 int, F_price2 int)
    insert A select   '普通',         10,                5
    union all select   '金卡',          9,               4
    union all select   '银卡',          8,                3create table B(F_ID int, F_specname nvarchar(10), F_Spec nvarchar(10))
    insert B select   1,        '盒子',            '规格1'
    union all select   2,        '盒子',            '规格2'
    union all select   3,        '盒子',            '规格3'declare @sql varchar(8000)
    set @sql='select '
    select @sql=@sql+quotename(col)+'='+quotename(colName+' |'+col, '''')+',' from
    (
    select colName='F_Price1', col=cast(F_Price1 as nvarchar)+'元' from A
    union all
    select 'F_Price2', cast(F_price2 as nvarchar)+'元' from A
    union all 
    select '盒子', F_Spec from B
    )tmp
    set @sql=left(@sql, len(@sql)-1)
    exec(@sql)
    --result
    10元            9元            8元            5元            4元            3元            规格1         规格2         规格3         
    -------------- ------------- ------------- ------------- ------------- ------------- ----------- ----------- ----------- 
    F_Price1 |10元  F_Price1 |9元  F_Price1 |8元  F_Price2 |5元  F_Price2 |4元  F_Price2 |3元  盒子 |规格1     盒子 |规格2     盒子 |规格3