Hello learners! Hope you all are doing fine. This article is about merging different row cells into one for the purpose of reporting. Sometimes we need to customize according to the client’s requirements.

So, for this purpose, I am demonstrating this article. Let’s get started.

First, we will create a table and fill it with some random data.

Merging Row Cells

Now, our table looks like this.

Merging Row Cells

And what I want to achieve is,

Merging Row Cells

Now, if you see, the rows are merged for one particular ID and the description for one ID is presented in a manner so that the name that belongs to one ID is shown in one cell:

  1. a
  2. b
  3. c

That means the number and the next name with the same ID comes in the next line. Please find the below code to see how we can achieve this. We would be needing two global temporary tables to achieve our purpose. I have already described why we need to use global temporary tables rather than local temporary tables in the previous article.

The local temporary tables run within the scope of the procedure, when we try to select the data from the local temporary table outside the procedure scope it will give you an error saying ‘’Invalid Object’’. That is the reason to use the global temporary table.

We will also be using cursors to achieve this result as it is used to fetch the records one by one. Here is the code:

  1. Go
  2. Create Proc USP_Merge_Different_Row_Cells_Into_One
  3. As
  4. Begin
  5. If Object_id('tempdb.dbo.##temp_merge') IS NOT NULL
  6. Drop Table ##temp_merge;
  7. If Object_id('tempdb.dbo.##temp_merge1') IS NOT NULL
  8. Drop Table ##temp_merge1;
  9. Create table ##temp_merge (ID int, Name varchar(Max));
  10. Create table ##temp_merge1 (ID int, Name varchar(Max));
  11. Insert into ##temp_merge values('','');
  12. Declare @b Varchar(max) ,
  13. @a Varchar(max),
  14. @k int,
  15. @i int = 0,
  16. @j int = 1;
  17. Declare S_Cur CURSOR FOR
  18. Select ID, Name from Test_Merge
  19. Open S_cur
  20. Fetch Next from S_Cur into @k,@a
  21. While @@FETCH_STATUS = 0
  22. Begin
  23. If(@k=@j)
  24. Begin
  25. Set @i = @i + 1
  26. Set @b = Cast(@i as varchar(2)) + '.' + space(2) + @a
  27. Update ##temp_merge
  28. Set id = @k ,name = Name + @b + char(13)
  29. Fetch next from S_Cur into @k, @a
  30. End
  31. Else If(@k <> @j)
  32. Begin
  33. Insert into ##temp_merge1
  34. Select * from ##temp_merge
  35. Set @j = @j + 1;
  36. Update ##temp_merge
  37. Set ID = '' ,Name = '';
  38. Set @i = 0
  39. End
  40. End
  41. Insert into ##temp_merge1
  42. Select * from ##temp_merge
  43. Close S_Cur
  44. Deallocate S_CUr
  45. Return
  46. End

Now, we will execute the procedure and check the result.

Merging Row Cells

Though the result you see does not show you the New Line character, when you copy the cell and paste in a blank space you will get what we wanted. Here is the copy result as well.


    1. a
    2. b
    3. c

    1. a
    2. b
    3. c

    1. a
    2. b
    3. c

For every ID there are 3 name values in one cell. I hope this helps. You can use this logic for reporting or any other purpose that meets the logic.

Any feedback will be welcomed and I will try and improve. Many thanks for your support. Happy Learning!