简体   繁体   中英

Query to Stored Procedure for SSRS Report

I created a report in SSRS using a query with parameters and it worked fine. But I decided to use a stored procedure instead so that if ever there is any changes needed, it would only need to be applied to SSMS and not both SSMS and the query in SSRS.

So I created a stored procedure like seen below:

USE [Some_Database]

GO

CREATE PROCEDURE MyReport 

AS

DECLARE @p_ServerName nvarchar(10) = 'all'
DECLARE @p_Env        nvarchar(10) = 'all'
DECLARE @p_EnvCat     nvarchar(10) = 'all'

SELECT DISTINCT <rest of my query...>

This is a greatly shortened version of what I am using but the ordering of the keywords is accurate. When I preview the report it in SSRS I get my No Rows Message.

Can anyone see what I did wrong in the creation of the stored procedure?

Also does using a stored procedure require different parameter setup in SSRS than if using a query?

Cheers.

UPDATE: After putting paramters between CREATE and AS and removing DECLARE, it works in SSRS for specific values but when I choose "ALL" it does not.

Any ideas?

The only thing I can think of is perhaps with where the parameters are positioned. If there are params being passed in from an outside source, like from a user inputing them in reporting services or something, then they need to be declared before the AS statement like so:

USE [CRCIVR]
GO
Create Proc dbo.[spFrontTileExceptionsData]  
(
    @userid as int,
    @companyid as int,
    @exceptioncodes as varchar
)        
AS
Begin     

That is all I can think of without seeing the rest of the code though. It could also be something within reporting services itself.

You'll want to make the stored procedure accept input parameters, in order for SSRS to send them in. If you DECLARE parameters within the proc, those are otherwise 'hidden' to SSRS. Using your example, it'd be something like this:

USE [Some_Database]

GO

CREATE PROCEDURE MyReport 
   @p_ServerName NVARCHAR(10) = 'all',
   @p_Env NVARCHAR(10) = 'all',
   @p_EnvCat NVARCHAR(10) = 'all'
AS

SELECT DISTINCT <rest of my query...>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM