question

cweber avatar image
cweber asked

Is my SP Reporting-Services Compatible?

Hello, I have finally managed to build a stored procedure per my previous question [ http://ask.sqlservercentral.com/questions/109193/t-sql-sp-change-rows-to-columns.html#answer-109225][1] I now must learn reporting services to present the results. I have used 'Crystal Reports' on the past and am hopeful that the learning curve for reporting services won't be too painful. **Q. Does anyone have any suggestions/advice for me, in advance of my studying reporting services, on any changes that I might need to make to the code below?** **I am specifically interested in the passing of the output and the calling of the SP.** CREATE PROCEDURE [Custom].[usp_ZS_PartsReport_Get] AS BEGIN DECLARE @msg VARCHAR(300), @err_code INT, @spver VARCHAR(6) = 'v1.0.0', @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); SET NOCOUNT ON BEGIN TRY -- Dynamic SQL SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.PartAttribute) FROM PartAttributesList c FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') SET @query = 'SELECT Part, CachedDescription, PartCategory, PartType, PartStatus, ' + @cols + ' FROM ( SELECT p.Part, p.CachedDescription, pal.PartAttribute, pa.AttributeValue, pc.PartCategory, pt.PartType, ps.PartStatus FROM [dbo].[Parts] AS p INNER JOIN [dbo].[PartAttributes] AS pa ON pa.PartId = p.PartId INNER JOIN [dbo].[PartAttributesList] AS pal ON pal.PartAttributeListID = pa.PartAttributeListID INNER JOIN [dbo].[PartCategories] AS pc ON pc.PartCategoryID = p.PartCategoryID INNER JOIN [dbo].[PartTypes] AS pt ON pt.PartTypeID = p.PartTypeID INNER JOIN [dbo].[PartStatus] AS ps ON ps.PartStatusID = p.PartStatusID WHERE ( (p.Deleted = 0) AND (pt.IsAssembly = 0)) ) ispart PIVOT ( MAX(AttributeValue) FOR PartAttribute IN (' + @cols + ') ) p ORDER BY Part ' -- Output Results to the 'Microsoft SQL Server Managemnet Studio' development Window EXECUTE (@query) END TRY BEGIN CATCH SELECT @msg = ERROR_MESSAGE(), @err_code = ERROR_NUMBER() RAISERROR @err_code @msg RETURN 1 END CATCH -- Write to Messages Panel of the 'Microsoft SQL Server Managemnet Studio' development Window PRINT 'Zone Substations Parts Report'; PRINT N'SQL Report Version: ' + @spver + ' '; PRINT N'Printed on: ' + RTRIM(CAST(GETDATE() AS nvarchar(30))); RETURN 0 END [1]: http://ask.sqlservercentral.com/questions/109193/t-sql-sp-change-rows-to-columns.html#answer-109225
stored-proceduresreporting-services
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Mister Magoo avatar image
Mister Magoo answered
If the only reason you are using dynamic sql is to pivot an unknown set of columns, then don't pivot at all in the SP, just let SSRS do that for you in a "matrix" report.
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

cweber avatar image cweber commented ·
I will look into that when I start learning SSRS. At the moment I needed data out quickly. PS: I moved the @cols to the end of the dynamic sql to overcome an issue with columns that I was encountering.
0 Likes 0 ·
Mister Magoo avatar image Mister Magoo commented ·
There is less to learn with a matrix report than there is with dynamic pivots...but each to their own...
0 Likes 0 ·
cweber avatar image cweber commented ·
Thanks for your reply. I was originally looking at Powershell and sqlcmd to provide the User with a method of reporting, hence trying to implement everything in code. SSRS appeared to be a viable alternative, but I've hesitated embarking on yet another learning path. From what you seem to be implying, SSRS may offer a reasonable time-to-market and offer a lot more flexibility. I've found a book by Brian Larson and am also chasing up other resources. Hopefully I can get things moving with SSRS and then try out your matrix suggestion.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.