0

I have been facing a problem for several days, I have not found a solution on the various forums and in the documentation that I have read

I need to create a SQL query that allows me to retrieve data from an Excel file. This file has a first column that I always have to retrieve, and 38 others columns which correspond to integers. I need to retrieve only one of these 38 columns using a user entered parameter in the SSRS report

For example, the Excel file has the following columns: Col1, 547, 589, 512, 596, ... I have to get "Col1" and "512" if the user entered 512 in the parameter field

The query I wrote works in SSMS, but as soon as I use it in the report the parameter no longer works

DECLARE @Param AS INT = 552

 DECLARE @SQL NVARCHAR(MAX)

 SET @SQL = 'SELECT * FROM OPENROWSET 
                    (''Microsoft.ACE.OLEDB.12.0'',''Excel 12.0 Xml;HDR=YES;Database=W:\Test\Test.xlsx''
                    ,''SELECT [Year] + 1 AS [Year] , ROUND([' + cast(@Param as varchar(10)) + ']/1000,0)  AS Val  FROM [Sheet1$]
                     
                        ;'') '
DECLARE @ParmDefinition NVARCHAR(500) = '@Param NVARCHAR(20)';

EXECUTE sp_executesql @SQL
,@ParmDefinition
,@Param = @Param

image1

If i let the parameter in the SQL query as state above, it will works, bun when i comment the declaration and use SSRS parameter it is recognized as null

screen2

screen3

This window appears and the parameters doesn't work

screen4

Does anyone have an idea why parameter passing doesn't work (it seems to be related to the fact that it's a dynamic query with an openrowset, is there an alternative?) Thanks in advance for the help

I tried to create a scalar function user but it doesn't work

7
  • A few things. 1. What doesn't work, what error do you get? 2. Does the SSRS service account have access to drive W? 3. Try using a UNC path instead of a mapped drive letter. 4. I assume the @Param declaration is commented out when you run this from SSRS, if not, it should be. Commented Mar 27, 2024 at 9:28
  • The dataset doesn't return any value when i comment the declaration of @Param and try to use SSRS parameter instead (the error is that parameter is recognized as NULL) .The SSRS service account has access because i can print result on the report when the parameter is not commented in SQL Query Commented Mar 27, 2024 at 9:41
  • The parameter passed in from SSRS is case sensitive so it must exactly match the SSRS parameter name. I'm confused though, you said you can print result when the param is not commented out, what result?. Can you edit your question and add all this info there, show screen shots of results in different scenarios etc. It might help narrow down where the problem is Commented Mar 27, 2024 at 10:19
  • I added some screens, I hope it will be easier to understand now. Thanks for your help Commented Mar 27, 2024 at 10:56
  • The parameter name is @position but that's not in the query. Maybe rename it @Param? Commented Mar 27, 2024 at 12:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.