我有一张表table1
 Id      c1      c2      c3       c4 
 1       100     100     100      100
 2       200     200     100      400
 3       100     30      30       200另一张表table2
 code       name
 001          c1
 002          c3
我要实现这样的结果
Id     c1         c31      100        100
2      200        100
3      100        30
就是在table1中的列跟 table2中的数据关联起来
哪位能帮我用select 语句实现一下
最好能写个存储过程  谢谢!

解决方案 »

  1.   

    格式显示的有点不正确
    看下面的我有一张表table1 
    Id      c1      c2      c3      c4 
    1      100    100    100      100 
    2      200    200    100      400 
    3      100    30      30      200 另一张表table2 
    code      name 
    001          c1 
    002        c3 
    我要实现这样的结果 
    Id    c1        c3
    1      100        100 
    2      200        100 
    3      100        30 
    就是在table1中的列跟 table2中的数据关联起来 
    哪位能帮我用select 语句实现一下 
    最好能写个存储过程  谢谢!
      

  2.   


    create proc tempas
    begin
       declare @str varchar(max),@sql varchar(max)
       set @str=stuff(replace(replace((select name from table2 for xml auto),'<table2 name="',','),'"/>',''),1,1,'')
       set @sql='select id,'+@str+' from table1' 
       exec(@sql)
    end

    警告:純手寫 未測試