其中传进来的字符串str  它的值是表格1 basecode字段的值,每个值是用","分开的 字段1是country_code  字段2名称是str第1个","后前面的值 字段3名称是str第2个","后前面的值,以后字段一次类推 
描述真不清楚。你别这样做了。你表二就两字段得了。
country_code  和 个随便什么的。后面的字段直接存你的str就行了。

解决方案 »

  1.   

    自己改成存储过程吧
    declare @p varchar(10)
    set @p = '5,6,7'if object_id('tempdb..#t') is not null drop table #t
    create table #t (country_code int,base_code int, money int)
    insert into #t
    select 1,5,98 union
    select 1,6,30 union
    select 1,7,55 union
    select 2,5,40 union
    select 2,7,88 union
    select 3,6,60 union
    select 3,1,60declare @sql varchar(1000)
    set @sql='select country_code'
    select @sql=@sql+',sum(case when base_code='+cast(base_code as varchar(10))+' then money else 0 end) as ['+cast(base_code as varchar(10))+']'
    from (select base_code 
             from #t 
             where charindex(','+cast(base_code as varchar(10))+',',','+@p+',')>0 
             group by base_code
            ) a
    set @sql=@sql+' from #t group by country_code'execute( @sql )
    /*
    country_code 5           6           7
    ------------ ----------- ----------- -----------
    1            98          30          55
    2            40          0           88
    3            0           60          0(3 行受影响)
    */
      

  2.   

    我是创建一个新的表格2,应该在存储过程写创建新的表。表的内容就是下面的
    /*
    country_code 5           6           7
    ------------ ----------- ----------- -----------
    1            98          30          55
    2            40          0           88
    3            0           60          0(3 行受影响)
    */