TABLE ACHAT : ID_ACHAT | ID_CLIENT | ACHAT_CLIENT
001 | 001 | fleur
002 | 002 | creme
003 | 002 | viande
004 | 002 | pain
005 | 003 | chocolat
DECLARE @int as int,@text as nvarchar(100)
DECLARE @tab table(id_client int, lesachats nvarchar(100))
DECLARE C1 CURSOR FOR
Select id_client
from achat
OPEN C1
set @int=0
FETCH NEXT FROM C1 into @int
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @textok as nvarchar(100)
DECLARE C2 CURSOR FOR
Select achat_client from achat where id_client = @int
set @textok = ''
OPEN C2
FETCH NEXT FROM C2 into @text
IF @@FETCH_STATUS = 0
BEGIN
SET @textok=@text
FETCH NEXT FROM C2 into @text
END
WHILE @@FETCH_STATUS = 0
BEGIN
SET @textok=@textok+char(10)+@text
FETCH NEXT FROM C2 INTO @text
END
CLOSE C2
DEALLOCATE C2
INSERT INTO @tab (id_client,lesachats) values (@int,@textok)
FETCH NEXT FROM C1 into @int
END
CLOSE C1
DEALLOCATE C1
SELECT * from @tab as tab order by tab.id_client
Si j'ai bien recopié le code on devrait avoir comme resultat
001 | fleur
002 | creme viande pain
003 | chocolat
Partager