dbo.Aggregate的用法,可否在Update语句中使用?

解决方案 »

  1.   

    我举个例子:
    有一张表 Student
      Id      class    name    age
       1        1        r      21
       2        1        q      22
       3        2        w      22我现在要向class表中插入student表的内容
      class          name
       1         r(21),q(22)
       2             w(22) class表的结构已经定义好了,只需要将数据更新进去。
      

  2.   

      class name
      1 A班
      2 B班这样就行了
      

  3.   


    create table tb1(id int,class char(1),name char(1),age int)
    insert into tb1
     select 1,'1','r',21 union all
    select 2,'1','q',22 union all
    select 3,'2','w',22 
    create table tb2(id char(1),name varchar(20))
    insert into tb2
    select '1',null union all
    select '2',null
    update a set name= stuff((select ','+name+'('+rtrim(age)+')' from tb1 where class=b.class for xml path('')),1,1,'') 
     from tb2 a left  join tb1 b on a.id= b.classselect * from  tb2 
    drop table tb1
    drop table tb2/*
    id   name
    ---- --------------------
    1    r(21),q(22)
    2    w(22)
      

  4.   


    有所帮助,不过代码中“for xml path('')”不太明白是啥意思?