在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
一个一个读出记录把name字段加起来,用SQL恐怕很难
Dim objCN As New ADODB.Connection Dim objRS As New ADODB.RecordsetobjCN.Open "Driver={SQL Server};Server=Develop;UID=SA;PWD=;Database=Test" Set objRS = objCN.Execute("select name from YourTable for xml raw")Debug.Print Replace(Replace(objRS(0).Value, "<row name=""", ""), """/>", "")objRS.Close Set objRS = NothingobjCN.Close Set objCN = Nothing
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
Dim objRS As New ADODB.RecordsetobjCN.Open "Driver={SQL Server};Server=Develop;UID=SA;PWD=;Database=Test"
Set objRS = objCN.Execute("select name from YourTable for xml raw")Debug.Print Replace(Replace(objRS(0).Value, "<row name=""", ""), """/>", "")objRS.Close
Set objRS = NothingobjCN.Close
Set objCN = Nothing