Hello Learners. I hope you all are doing fine. This particular article is about updating the dynamic columns on the go. There are certain situations where you have a table and then you have to pivot the table depending upon the data in any specific column, and you may have to update those columns as well.

In this article, I will be creating a table, pivoting it based on the values of a specific column, and then showing you how to update the values of such columns as well.

Let’s get started.

First, we will create a Test_Pivot Table and insert random data into the table.
Test_Pivot Table
Let’s check the result of the table.
result
Now, we have to pivot this table on the basis of Name Column. So, we will run this code to make it a pivot on the basis of Name column.
code
Here, I will show you how this query will look when we print this Dynamic SQL variable @M.
Dynamic SQL variable
When you check the Global Temporary Table ##Temp you will see the data is pivoted. Here it is.
Global Temporary Table
Now, we have dynamically inserted the data in the Global Temporary Table and checked the records. There are NULLs in the table so now our next step would be to update the NULLs to ZEROs as it is not presentable. Furthermore, this is just a simple example of how to update the values of Dynamic Columns on the go. You can use this logic and customize accordingly to your needs.

So, here is the code to update the table values dynamically.
  1. -- @I IS SET TO 2 AS THERE WOULD BE A DELIMITER AFTER EACH STRING OR IF YOU SET IT TO 1, ADD PLUS 1 TO THE COUNTER AT THE END
  2. DECLARE @I Int = 2,
  3. @K Int = LEN(@S), -- SET @K AS THE LENGTH OF VARIABLE @S
  4. @SQL NVarchar(MAX)
  5. WHILE (@I < @K)
  6. BEGIN
  7. DECLARE @A Varchar(20);
  8. -- IF THERE IS A CHARINDEX IN THE STRING THIS WILL RUN
  9. IF ((SELECT CHARINDEX(',',@S)) !=0)
  10. BEGIN
  11. SET @A = SUBSTRING(@S,1,CHARINDEX(',',@S)-1);
  12. SET @S = (SELECT REPLACE(@S,@A+',',''));
  13. END
  14. --IF THERE IS NO CHARINDEX IN THE STRING THIS WILL RUN
  15. ELSE IF ((SELECT CHARINDEX(',',@S)) =0) BEGIN
  16. SET @A = @S
  17. --SET @A = SUBSTRING(@S,1,LEN(@S))
  18. END
  19. Print @A
  20. --DYNAMIC SQL TO UPDATE THE CONTENTS OF TEMPORARY TABLE
  21. SET @SQL = 'UPDATE ##Temp SET '+@A+' = 0 WHERE '+@A+' IS NULL'; EXEC SP_ExecuteSql @SQL --EXECUTING DYNAMIC SQL
  22. --INCREMENTING @I WITH THE LENGTH OF THE SINGLE COLUMN EXTRACTED FROM THE STRING OF DYNAMIC COLUMNS
  23. SET @I = @I + LEN(@A);
  24. END
I apologize for the editing. But please see the entire code for updating the values of Dynamic Columns. Below is the complete code.
  1. -- UPDATING DYNAMIC COLUMNS ON THE GO --
  2. /*
  3. --CREATE A TEST TABLE
  4. CREATE TABLE Test_Pivot
  5. ( [Year] Int,
  6. Name Varchar(10),
  7. Sales Int)
  8. --INSERT RANDOM VALUES
  9. INSERT INTO Test_Pivot VALUES
  10. (2010,'A',100),
  11. (2010,'B',300),
  12. (2010,'C',500),
  13. (2010,'D',600),
  14. (2011,'A',300),
  15. (2011,'B',200),
  16. (2011,'D',700),
  17. (2012,'B',400),
  18. (2012,'C',900)
  19. */
  20. GO
  21. ALTER PROC USP_UPDATE_DYNAMIC_COLUMNS
  22. AS
  23. BEGIN
  24. -- DECLARING VARIABLES
  25. DECLARE @S Varchar(MAX);
  26. SET @S = STUFF((SELECT DISTINCT ','+Name FROM Test_Pivot FOR XML PATH('')),1,1,'') -- INSERTING STRING WITH THE DYNAMIC COLUMNS NAME
  27. --PRINT @S
  28. -- INSERTING THE PIVOT DATA INTO TEMPORARY TABLE
  29. IF OBJECT_ID('TEMPDB.DBO.##Temp') IS NOT NULL
  30. DROP TABLE ##Temp;
  31. DECLARE @M nVARCHAR(MAX)
  32. SET @M ='
  33. SELECT * INTO ##Temp
  34. FROM
  35. (
  36. SELECT Yr as [Year],' + @S +
  37. ' FROM Test_Pivot
  38. Pivot
  39. (
  40. SUM(Sales) FOR Name IN('+ @S + ')
  41. )AS Pivot_Table
  42. )AS TAB
  43. '
  44. EXEC SP_EXECUTESQL @M
  45. -- @I IS SET TO 2 AS THERE WOULD BE A DELIMITER AFTER EACH STRING OR IF YOU SET IT TO 1, ADD PLUS 1 TO THE COUNTER AT THE END
  46. DECLARE @I Int = 2,
  47. @K Int = LEN(@S), -- SET @K AS THE LENGTH OF VARIABLE @S
  48. @SQL NVarchar(MAX)
  49. WHILE (@I < @K)
  50. BEGIN
  51. DECLARE @A Varchar(20);
  52. IF ((SELECT CHARINDEX(',',@S)) !=0) -- IF THERE IS A CHARINDEX IN THE STRING THIS WILL RUN
  53. BEGIN
  54. SET @A = SUBSTRING(@S,1,CHARINDEX(',',@S)-1);
  55. SET @S = (SELECT REPLACE(@S,@A+',',''));
  56. END
  57. --IF THERE IS NO CHARINDEX IN THE STRING THIS WILL RUN
  58. ELSE IF ((SELECT CHARINDEX(',',@S)) =0)
  59. BEGIN
  60. SET @A = @S
  61. --SET @A = SUBSTRING(@S,1,LEN(@S))
  62. END
  63. Print @A
  64. --DYNAMIC SQL TO UPDATE THE CONTENTS OF TEMPORARY TABLE
  65. SET @SQL = 'UPDATE ##Temp SET '+@A+' = 0 WHERE '+@A+' IS NULL'; EXEC SP_ExecuteSql @SQL --EXECUTING DYNAMIC SQL
  66. --INCREMENTING @I WITH THE LENGTH OF THE SINGLE COLUMN EXTRACTED FROM THE STRING OF DYNAMIC COLUMNS
  67. SET @I = @I + LEN(@A);
  68. END
  69. END
  70. EXEC USP_UPDATE_DYNAMIC_COLUMNS
  71. SELECT * FROM ##TEMP
So this is how we can update the dynamic columns dynamically.

Please note that we have used a Global Temporary Table rather than a Local Temporary Table. There is a reason for using this, i.e., the ‘Select * from #Temp ‘ after the EXEC SP_ExecuteSql will not run because Select * from #Temp will be out of the scope of the EXEC SP_ExecuteSql. So, this is the reason to use Global Temporary Table instead of Local Temporary Table.

Now, if we see the result of ##TEMP, we will see that the NULLs are replaced by ZEROs.
result of ##TEMP
Any feedback will be welcomed in order to improve the query or code. I hope this will help. Happy Learning!