开发者

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.

  1. Create a table named dbo.Consumer_Premium and stored procedure named dbo.GetData using the scripts provided under SQL Scripts section. Populate the table with data as shown in screenshot #1.

  2. Create an SSRS report named BindWhere.rdl as shown in screenshot #2.

  3. Create a report parameter named Gender with settings as shown in screenshots #3 - #5.

  4. Configure the report data source as shown in screenshots #6 - #8.

  5. Screenshot #9 shows default report execution with parameter Gender set to value Male.

  6. 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:

Can't bind WHERE part in a SQL query

Screenshot #2:

Can't bind WHERE part in a SQL query

Screenshot #3:

Can't bind WHERE part in a SQL query

Screenshot #4:

Can't bind WHERE part in a SQL query

Screenshot #5:

Can't bind WHERE part in a SQL query

Screenshot #6:

Can't bind WHERE part in a SQL query

Screenshot #7:

Can't bind WHERE part in a SQL query

Screenshot #8:

Can't bind WHERE part in a SQL query

Screenshot #9:

Can't bind WHERE part in a SQL query

Screenshot #10:

Can't bind WHERE part in a SQL query

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜