在vb中写代码可能还比较简单一点。如果一定要用SQL来实现,比较烦琐,用一个SQL是写不出来的。在SQL Server中可以用游标来实现:
declare tbl_aa cursor
for
select [id] from tablename order by [id]
open tbl_aa
declare @id int, @temp nvarchar(255)
select @temp=''
fetch next from tbl_aa into @id
while(@@fetch_status=0)
begin
select @temp=@temp+rtrim([name]) from tablename
where [id]=@id order by [id]
fetch next from tbl_aa into @id
end
close tbl_aa
deallocate tbl_aa
print @temp