想对数据库表PCode的字段PCID进行整理!表PCode:PCID
P11234
P11212
P11324
P12334
P12445
P12556
P23445
P23555想对字段PCID
前3个字符为P11所有值加入到临时表A的字段a1里
前3个字符为P12所有值加入到临时表A的字段a2里
前3个字符为P23所有值加入到临时表A的字段a3里
前3个字符为P24所有值加入到临时表A的字段a4里表A的最终结果如下所示:
A:
 a1       a2      a3      a4
P11234  P12334  P23445
P11212  P12445  P23555
P11324  P12556请问要怎么做会比较好些?谢谢!

解决方案 »

  1.   

    你的想法似乎有点问题,我想问的是A表中每条记录表示什么呢? 欢迎到QQ群16587582.可以大家讨论
      

  2.   

    这个好办:以下两个表内容你在你的数据库中稍加修改,出来的数据就是你所说的那种格式了,你可参照这两个表先试做一下
    create table grade(id int identity(1,1) primary key, name varchar(10) , subject varchar(50) ,grade decimal)
    create table subject(subject varchar(50) primary key)insert into subject values('math')
    insert into subject values('Eng.')
    insert into subject values('Chi.')
    insert into subject values('chem.')insert into grade values('yuan', 'math', 100)
    insert into grade values('yuan', 'Eng.', 70)
    insert into grade values('yuan', 'Chi.', 80)
    insert into grade values('yuan', 'chem.', 90)--存储过程脚本内容
    declare @Sql nvarchar(4000)set @Sql = 'select name 'select @Sql = @Sql + ', sum(case subject when ''' + subject + ''' then grade else 0 end) as [' + subject + ']'
    from subject 
    order by subjectprint @Sql set @Sql = @Sql + ' into ##result from grade group by name'print @Sql exec(@Sql)select * from ##result
    就行了
      

  3.   

    insert A(a1,a2,a3,a4)
    select case when substr(pcid,0,3) = p11 then pcid end a_a1,
           case when substr(pcid,0,3) = p12 then pcid end a_a2,
           case when substr(pcid,0,3) = p23 then pcid end a_a3,
           case when substr(pcid,0,3) = p24 then pcid end a_a4
    from PCode
      

  4.   

    只是給你一個見意,下面代碼可能會對你有幫助
    select (case when PCID=p1% then PCID else null end) as a1,
           (case when PCID=p2% then PCID else null end) as a2,
           (case when PCID=p3% then PCID else null end) as a3,
           (case when PCID=p4% then PCID else null end) as a4,
           .............
          from Table
      

  5.   

    我试了一下,应该是你要的格式。
    呵呵,可能写的麻烦了,权当参考:)
    #temptable 中的数据就是你想要要的,再将#temptable中的数据导入到你想插入的表中就可以了
    试试吧!/*
    create table #temptable
    (
    ident int,
    a1 varchar(50),
    a2 varchar(50),
    a3 varchar(50),
      a4 varchar(50)
    )*/
    declare @t1 integer
    declare @t2 integer
    declare @t3 integer
    declare @t4 integerset @t1 = 1
    set @t2 = 1
    set @t3 = 1
    set @t4 = 1declare @str varchar(50)
    declare @colname varchar(50)
    declare A_Cursor cursor for select PCid from PCode
    open A_Cursor
    while ( 1 > 0)
    begin
       fetch next from A_Cursor into @str
       if ( @@FETCH_STATUS <> 0 ) break
       --插入操作
       if (substring(@str,1,3) = 'p11') 
       begin
         print('@1')
         if exists( select * from #temptable where ident = @t1 )  
         begin
    update #temptable
            set a1 = @str
            where ident = @t1
         end
         else
         begin
            insert into #temptable (ident,a1) values (@t1,@str)
         end
         set @t1 = @t1+1
       end
       if (substring(@str,1,3) = 'p12') 
       begin
         print('@2')
         if exists( select * from #temptable where ident = @t2 )  
         begin
    update #temptable
            set a2 = @str
            where ident = @t2
         end
         else
         begin
            insert into #temptable (ident,a2) values (@t2,@str)
         end
         set @t2 = @t2+1
       end
       if (substring(@str,1,3) = 'p23') 
       begin
         print('@3')
         if exists( select * from #temptable where ident = @t3 )  
         begin
    update #temptable
            set a3 = @str
            where ident = @t3
         end
         else
         begin
            insert into #temptable (ident,a3) values (@t3,@str)
         end
         set @t3 = @t3+1
       end
       if (substring(@str,1,3) = 'p24') 
       begin
         print('@4')
         if exists( select * from #temptable where ident = @t4 )  
         begin
    update #temptable
            set a4 = @str
            where ident = @t4
         end
         else
         begin
            insert into #temptable (ident,a4) values (@t4,@str)
         end
         set @t4 = @t4+1
       end
       --插入操作结束
       print(@str)
    end
    close A_Cursor
    deallocate A_Cursorselect * from #temptable
    --drop table #temptable