Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have an issue in below stored procedure.

below Store procedure is working fine. But fixed the Year.

Any one know How can do the year dynamically select by user?

Pls advice

Maideen


SQL
SELECT * FROM ( SELECT salesEXE,SalesEXEName,name,gross,YearNo FROM vw_Pivot_Month_ALL) TableYearNo
pivot (Sum(gross) for [year] in ([2008],[2009],[2010],[2011],[2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019],[2020])
)pivotTable
Posted

Pass the year as a stored procedure parameter and run a query against this parameter.
E.g

Create SP_1 as
@Year as char(4)
AS
SELECT * FROM ( SELECT salesEXE,SalesEXEName,name,gross,YearNo FROM vw_Pivot_Month_ALL) TableYearNo
pivot (Sum(gross) for [year] in (@Year)
)pivotTable
 
Share this answer
 
Comments
Maideen Abdul Kader 8-Jul-14 22:00pm    
Hi abhinav

I have tried, But error Incorrect syntax near '@Year'.

Below is my SP

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_pivot_by_Year_AGENT]
@year varchar(4)
AS
BEGIN
SET NOCOUNT ON;

truncate table tbl_Pivot_AGENT
insert into tbl_Pivot_AGENT

SELECT * FROM ( SELECT salesEXE,SalesEXEName,name,gross,[Year] FROM vw_Pivot_Month_ALL) TableYearNo
pivot (Sum(gross) for [year] in (@year)
)pivotTable

END

Pls advice
Maideen
SQL
DECLARE @Year nvarchar(max)='[2008],[2009],[2010],[2011],[2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019],[2020]' -- here you can provide your dynamic values.
DECLARE @QUery nvarchar(max)='SELECT * FROM ( SELECT salesEXE,SalesEXEName,name,gross,YearNo FROM vw_Pivot_Month_ALL) TableYearNo
pivot (Sum(gross) for YearNo in ('+@Year+')
)pivotTable'

EXECUTE sp_executesql @QUery
 
Share this answer
 
Comments
Atyant Srivastava 9-Jul-14 0:31am    
variables defined is just for reference purpose please use the variable length accordingly.
As you asked in your question this is the way you can do it dynamically. Hope this helps!!
Please accept as Solution if solves the Purpose.
Maideen Abdul Kader 10-Jul-14 19:17pm    
Thanks Atyant

As per advice I did the Stored procedure. Working fine. But only in single value like '2011' cannot be range value
like 2010 to 2013.

Pls Advice.
Thank You.


Below is my SP

ALTER PROCEDURE [dbo].[usp_pivot_by_Year_AGENT]
@vYEAR varchar(100)
AS
BEGIN
SET NOCOUNT ON;

Declare @sSQL varchar(1000)

Set @sSQL = 'SELECT * FROM
(SELECT salesEXE,SalesEXEName,name,gross,YearNo FROM vw_Pivot_Month_ALL) TableYearNo
pivot (Sum(gross) for YearNo IN (['+@vYEAR+'])) pivotTable'

exec(@sSQL)
Atyant Srivastava 21-Jul-14 0:11am    
hi, what you are passing in @vYEAR, please trying removing YearNo IN (['+@vYEAR+'])) to
YearNo IN ('+@vYEAR+'), i hope you are using comma in the input as i have suggested above, please make sure SET @Year = '[2008],[2009],[2010],[2011],[2012],[2013],[2014],[2015],[2016],[2017],[2018],[2019],[2020]' goes like this only.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900