Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
maifs
Yak Posting Veteran
57 Posts |
Posted - 2010-05-06 : 03:22:35
|
| how can i concatenate the string with keeping previous values.i mean to say that i am using cursor for iteration and changing of values.for example: a variable having value 'a' . each iteration change its value.On next iteration value ll be change of that variable e.g: 'b' in that variable.so i want to concatenate these all variables into one.its output should be like as:'a','b','c' and so on |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-05-06 : 03:33:31
|
set @var = @var + next_valueIf that isn't what you want to know then be more specific please. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
maifs
Yak Posting Veteran
57 Posts |
Posted - 2010-05-06 : 04:00:16
|
| thanx dear now my problem has been solved.i was just concatenating the same string not next and previous example.my problem has been resolved from this example:DECLARE @tbl TABLE (id INT PRIMARY KEY, list VARCHAR(8000)) SET NOCOUNT ON DECLARE @c INT, @p VARCHAR(8000), @cNext INT, @pNext VARCHAR(40) DECLARE c CURSOR FOR SELECT CategoryId, ProductName FROM Northwind..Products ORDER BY CategoryId, ProductName ; OPEN c ; FETCH NEXT FROM c INTO @cNext, @pNext ; SET @c = @cNext ; WHILE @@FETCH_STATUS = 0 BEGIN IF @cNext > @c BEGIN INSERT @tbl SELECT @c, @p ; SELECT @p = @PNext, @c = @cNext ; END ELSE SET @p = COALESCE(@p + ',', SPACE(0)) + @pNext ; FETCH NEXT FROM c INTO @cNext, @pNext END INSERT @tbl SELECT @c, @p ; CLOSE c ;DEALLOCATE c ;SELECT * FROM @tbl ; |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2010-05-06 : 09:58:36
|
u can try xml path ('')SELECT CategoryId, SUBSTRING((SELECT CAST(ProductName AS VARCHAR)+',' FROM Northwind..Products b WHERE a.CategoryId=b.CategoryId FOR XML PATH('')), 0, LEN(SELECT CAST(ProductName AS VARCHAR)+',' FROM Northwind..Products b WHERE a.CategoryId=b.CategoryId FOR XML PATH(''))-1)FROM Northwind..Products aGROUP BY CategoryId Hope can help...but advise to wait pros with confirmation... |
 |
|
|
|
|
|
|
|