ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.1k

How to call sp and pass parameters to make pivot on sample below?

Jan 7 2021 4:17 PM
I can't call sp and how to pass parameter to make pivot to temp table below
so can you help me execute this sp and get result
stored procedure name [dbo].[rs_pivot_table]
  1. Create Procedure [dbo].[rs_pivot_table]  
  2. @schema sysname=dbo,  
  3. @table sysname,  
  4. @column sysname,  
  5. @agg nvarchar(max),  
  6. @sel_cols varchar(max),  
  7. @new_table sysname,  
  8. @add_to_col_name sysname=null  
  9. As  
  10. --Exec dbo.rs_pivot_table dbo,##TEMPORAL1,tip_liq,'sum([val_liq]),sum([can_liq]),','cod_emp,cod_con,tip_liq',##TEMPORAL1PVT,'hola';  
  11. Begin  
  12. Declare @query varchar(max)='';  
  13. Declare @aggDet varchar(100);  
  14. Declare @opp_agg varchar(5);  
  15. Declare @col_agg varchar(100);  
  16. Declare @pivot_col sysname;  
  17. Declare @query_col_pvt varchar(max)='';  
  18. Declare @full_query_pivot varchar(max)='';  
  19. Declare @ind_tmpTbl int--Indicador de tabla temporal 1=tabla temporal global 0=Tabla fisica  
  20. Create Table #pvt_column(  
  21. pivot_col varchar(100)  
  22. );  
  23. Declare @column_agg table(  
  24. opp_agg varchar(5),  
  25. col_agg varchar(100)  
  26. );  
  27. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@tableAND type in (N'U'))  
  28. Set @ind_tmpTbl=0;  
  29. ELSE IF OBJECT_ID('tempdb..'+ltrim(rtrim(@table))) IS NOT NULL  
  30. Set @ind_tmpTbl=1;  
  31. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(@new_table) AND type in (N'U')) OR  
  32. OBJECT_ID('tempdb..'+ltrim(rtrim(@new_table))) IS NOT NULL  
  33. Begin  
  34. Set @query='DROP TABLE '+@new_table+'';  
  35. Exec (@query);  
  36. End;  
  37. Select @query='Select distinct '+@column+' From '+(case when @ind_tmpTbl=1 then 'tempdb.' else '' end)+@schema+'.'+@table+' where '+@column+' is not null;';  
  38. Print @query;  
  39. Insert into #pvt_column(pivot_col)  
  40. Exec (@query)  
  41. While charindex(',',@agg,1)>0  
  42. Begin  
  43. Select @aggDet=Substring(@agg,1,charindex(',',@agg,1)-1);  
  44. Insert Into @column_agg(opp_agg,col_agg)  
  45. Values(substring(@aggDet,1,charindex('(',@aggDet,1)-1),ltrim(rtrim(replace(substring(@aggDet,charindex('[',@aggDet,1),charindex(']',@aggDet,1)-4),')',''))));  
  46. Set @agg=Substring(@agg,charindex(',',@agg,1)+1,len(@agg))  
  47. End  
  48. Declare cur_agg cursor read_only forward_only local static for  
  49. Select  
  50. opp_agg,col_agg  
  51. from @column_agg;  
  52. Open cur_agg;  
  53. Fetch Next From cur_agg  
  54. Into @opp_agg,@col_agg;  
  55. While @@fetch_status=0  
  56. Begin  
  57. Declare cur_col cursor read_only forward_only local static for  
  58. Select  
  59. pivot_col  
  60. From #pvt_column;  
  61. Open cur_col;  
  62. Fetch Next From cur_col  
  63. Into @pivot_col;  
  64. While @@fetch_status=0  
  65. Begin  
  66. Select @query_col_pvt='isnull('+@opp_agg+'(case when '+@column+'='+quotename(@pivot_col,char(39))+' then '+@col_agg+  
  67. ' else null end),0) as ['+lower(Replace(Replace(@opp_agg+'_'+convert(varchar(100),@pivot_col)+'_'+replace(replace(@col_agg,'[',''),']',''),' ',''),'&',''))+  
  68. (case when @add_to_col_name is null then space(0) else '_'+isnull(ltrim(rtrim(@add_to_col_name)),''end)+']'  
  69. print @query_col_pvt  
  70. Select @full_query_pivot=@full_query_pivot+@query_col_pvt+', '  
  71. --print @full_query_pivot  
  72. Fetch Next From cur_col  
  73. Into @pivot_col;  
  74. End  
  75. Close cur_col;  
  76. Deallocate cur_col;  
  77. Fetch Next From cur_agg  
  78. Into @opp_agg,@col_agg;  
  79. End  
  80. Close cur_agg;  
  81. Deallocate cur_agg;  
  82. Select @full_query_pivot=substring(@full_query_pivot,1,len(@full_query_pivot)-1);  
  83. Select @query='Select '+@sel_cols+','+@full_query_pivot+' into '+@new_table+' From '+(case when @ind_tmpTbl=1 then 'tempdb.' else '' end)+  
  84. @schema+'.'+@table+' Group by '+@sel_cols+';';  
  85. print @query;  
  86. Exec (@query);  
  87. End;  
  88. GO  
  89. CREATE TABLE #yt  
  90. (  
  91. [Store] int,  
  92. [Week] int,  
  93. [xCount] int  
  94. );  
  95. INSERT INTO #yt  
  96. (  
  97. [Store],  
  98. [Week], [xCount]  
  99. )  
  100. VALUES  
  101. (102, 1, 96),  
  102. (101, 1, 138),  
  103. (105, 1, 37),  
  104. (109, 1, 59),  
  105. (101, 2, 282),  
  106. (102, 2, 212),  
  107. (105, 2, 78),  
  108. (109, 2, 97),  
  109. (105, 3, 60),  
  110. (102, 3, 123),  
  111. (101, 3, 220),  
  112. (109, 3, 87);  
Expected result as below
  1. Store 1 2 3 4 5 6....  
  2. -----  
  3. 101 138 282 220  
  4. 102 96 212 123  
  5. 105 37  
  6. 109  

Answers (4)