Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

sql - Report Builder not listing any fields from query using exec(@customquery) to output

Using SSMS, the code below works perfectly fine. When I input the code into a Dataset in SSRS, the report data view doesn't show any of the fields the exec() function outputs. I use these fields in other areas of the report to filter and sort by, so the report is currently broken. I have looked into other questions on here, and they don't address this situation. If there is a way to store this query as a stored procedure and then call it in SSRS, that will work for me. I just don't know how to do that.

Declare @YR2 VARCHAR(MAX), @YR1 VARCHAR(MAX)
Declare @MT2 VARCHAR(MAX), @MT1 VARCHAR(MAX)
Declare @Query VARCHAR(MAX)
SET @YR2 = YEAR(dateadd(month, datediff(month, 0, getdate())-2, 0))
SET @YR1 = YEAR(dateadd(month, datediff(month, 0, getdate())-1, 0))
SET @MT2 = FORMAT(MONTH(dateadd(month, datediff(month, 0, getdate())-2, 0)),'00')
SET @MT1 = FORMAT(MONTH(dateadd(month, datediff(month, 0, getdate())-1, 0)),'00')

Set @Query = 'Select * FROM 
             (Select '+@MT2+'.'+@YR2+' as [Starting Point],ACCT_NBR as [Account],NBR_DUE From dbo.EOM_DB99_CalculatedValues_' 
             + @YR2 + @MT2 + ' UNION ALL
              Select '+@MT1+'.'+@YR1+' as [Starting Point],ACCT_NBR as [Account],NBR_DUE From dbo.EOM_DB99_CalculatedValues_' 
             + @YR1 + @MT1 + ') a Where a.Account IN (Accounts)'

Exec(@Query)

I was needing to automate a report that always uses the previous 2 EOM tables from the database and this was the only way I could get it to work in SSMS. Now I just need it to work in Report Builder 2014. I am open to ideas!

output

question from:https://stackoverflow.com/questions/65649598/report-builder-not-listing-any-fields-from-query-using-execcustomquery-to-out

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SSRS may not be able to determine the fields without executing the query you you could do two things..

NOTE: Both of these assume that the field names and datatype will always be the same. SSRS expects the same result structure each time.

Options 1:

Create a temp table with the desired structure and stick the results in there like this (swap column names and datatypes out as required)

...
Set @Query = 'Select * FROM 
             (Select '+@MT2+'.'+@YR2+' as [Starting Point],ACCT_NBR as [Account],NBR_DUE From dbo.EOM_DB99_CalculatedValues_' 
             + @YR2 + @MT2 + ' UNION ALL
              Select '+@MT1+'.'+@YR1+' as [Starting Point],ACCT_NBR as [Account],NBR_DUE From dbo.EOM_DB99_CalculatedValues_' 
             + @YR1 + @MT1 + ')'
...
CREATE TABLE #t (StartingPoint varchar(10), Account varchar(20), NBR_DUE INT)

INSERT INTO #t
    EXEC(@Query)

SELECT * FROM #t WHERE Account IN (@Accounts)

NOTE: @Accounts is a multi-value parameter p[assed in from the report.

As the structure of #t is defined in the query SSRS will accept this without any problems. As I said, you will have to change datatype and names to suit but hopefully that will give you enough to solve the problem.

Option 2:

Add all the fields manually to the dataset - I 'think' this will work but I have not tried it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...