I used Common Table Expressions to write a pivot table so I can dynamically capture current year in displaying results.
For instance, each of our members is required to pay annual dues each year.
So, to display current dues year, we use something like this:
Member Name, Date Registered, Amount Owed, Amount Paid, Remaining Balance 2019 2020 2021.
Jane Doe 07/01/2019 120.00 70.00 50.00 50.00 0.00 0.00
This works just fine.
Where we are running into trouble is when we use reporting services (using visual studio report viewer) to display this results.
I am getting error that the years (2019, 2020, 2021) are not valid field names.
I even tried using [] as in [2019]… but still no love.
Any work around to this?
Any assistance is greatly appreciated.
USE [myDB]
GO
/****** Object: StoredProcedure [dbo].[uspGetPivotedData]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('MemberHistory', 'U') IS NOT NULL
DROP TABLE MemberHistory;
GO
ALTER PROCEDURE [dbo].[uspGetPivotedData]
AS
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(YEAR(t.EventYear))
FROM Transactions t
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'');
set @query = N'WITH CTE AS
(
SELECT
t.TransactionID,
m.memberName,
m.date_registered,
envelopeNumber,
registrationFee,
Amount,
YEAR(eventyear) eventyear,
CONVERT(varchar, CAST(t.AmountPaid AS money), 1) AS AmountPaid,
t.Balance
FROM
dbo.Transactions AS t
INNER JOIN dbo.Members AS m
ON t .MemberID = m.MemberID
INNER JOIN dbo.PaymentTypes AS p
ON t .TypeID = p.PaymentTypeID
)
SELECT
TransactionID,
memberName,
date_registered,
envelopeNumber,
CONVERT(varchar, CAST(registrationFee AS money), 1) AS registrationFee,
Amount,
' + @cols + N',
CONVERT(varchar, CAST(Balance AS money), 1) AS Balance
INTO MemberHistory
FROM
CTE
PIVOT
(
MAX(AmountPaid)
FOR eventyear IN (' + @cols + N')
) p
ORDER BY
TransactionID';
EXECUTE sp_executesql @query;EXECUTE sp_executesql @query;
After testing the similar data in my page based on your needs, I found that in ssrs
data format is not allowed as field name such as your fields [2017],][2018],[2019] etc.
I recommend that you when you pivot year to fields, you should convert it to varchar format, then in the dataset fields , you should change the number FieldName to string type like below:
Best Regards,
YongQing.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
259 Points
1191 Posts
How can I use Number as field name in reporting services?
Sep 11, 2019 08:31 PM|simflex|LINK
Greetings experts,
I used Common Table Expressions to write a pivot table so I can dynamically capture current year in displaying results.
For instance, each of our members is required to pay annual dues each year.
So, to display current dues year, we use something like this:
This works just fine.
Where we are running into trouble is when we use reporting services (using visual studio report viewer) to display this results.
I am getting error that the years (2019, 2020, 2021) are not valid field names.
I even tried using [] as in [2019]… but still no love.
Any work around to this?
Any assistance is greatly appreciated.
Many thanks in advance
Contributor
3710 Points
1043 Posts
Re: How can I use Number as field name in reporting services?
Sep 12, 2019 08:19 AM|Yongqing Yu|LINK
Hi simflex,
According to your description, for this error message, I recommend you change table control to martix control to solve it.
How to use martix in ssrs, you can refer to this link :
Create a Matrix (Report Builder and SSRS)
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
259 Points
1191 Posts
Re: How can I use Number as field name in reporting services?
Sep 12, 2019 02:53 PM|simflex|LINK
YongQing,
Thanks very much for your response.
The same error exists whether I use matrix/Tablix or not.
Sorry to post the long code but this is where the error is occurring.
Any instance of 2019 is causing an error even as I am using matrix.
Contributor
3710 Points
1043 Posts
Re: How can I use Number as field name in reporting services?
Sep 18, 2019 09:03 AM|Yongqing Yu|LINK
Hi simflex,
After testing the similar data in my page based on your needs, I found that in ssrs data format is not allowed as field name such as your fields [2017],][2018],[2019] etc.
I recommend that you when you pivot year to fields, you should convert it to varchar format, then in the dataset fields , you should change the number FieldName to string type like below:
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.