Can't bind WHERE part in a SQL query
When you try to create this proc on MS SQL Server 2008 and try to bind a report (Crystal or SQL Server reporting) you can't get any parameter from the report. I don't get any errors. I just see the proc name and no column under it to get any data from it. Please help
create proc getdata
@where as nvarchar (max)
as
declare @sql as varchar(max)
set @sql='select cp.address,
(case when cp.male =1 then ''Male''
开发者_Go百科 when cp.female =1 then ''Female''
else null end ) AS "Gender"
from consumer_premium cp where '+ @where
exec(@sql);
@where example execution
getdata 'male=1'
thats the table
CREATE TABLE [dbo].[Consumer_Premium](
[Address] [nvarchar](255) NULL,
[Male] [bit] NULL,
[Female] [bit] NULL)
please help.
Here is one possible option that you can try if you are using SSRS 2008
. Following example was created using SSRS 2008 based on the data provided in the question.
Create a table named
dbo.Consumer_Premium
and stored procedure nameddbo.GetData
using the scripts provided under SQL Scripts section. Populate the table with data as shown in screenshot #1.Create an SSRS report named
BindWhere.rdl
as shown in screenshot #2.Create a report parameter named
Gender
with settings as shown in screenshots #3 - #5.Configure the report data source as shown in screenshots #6 - #8.
Screenshot #9 shows default report execution with parameter
Gender
set to value Male.Screenshot #10 shows default report execution when the parameter
Gender
value is changed to Female. You need to click on the View Report button to refresh the data.
Hope that helps.
SQL Scripts:
CREATE TABLE [dbo].[Consumer_Premium](
[Address] [nvarchar](255) NULL,
[Male] [bit] NULL,
[Female] [bit] NULL
) ON [PRIMARY]
GO
CREATE PROCEDURE getdata
(
@where AS NVARCHAR(MAX)
)
AS
DECLARE @sql AS VARCHAR(MAX)
SET @sql='select cp.address,
(case when cp.male =1 then ''Male''
when cp.female =1 then ''Female''
else null end ) AS "Gender"
from consumer_premium cp where '+ @where
EXEC(@sql);
GO
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
精彩评论