报告时出现sql查询问题

时间:2011-06-29 12:18:30

标签: sql sql-server sql-server-2008 ssrs-2008

我有这个SQL查询,它一切正常,但我想使用参数解析'where part'(@ged) 当我在sql中尝试这个工作正常..但我不能让它在水晶报告或SQL报告服务中工作 在此查询中.. @ J_Sec是一个参数,@ged是where statment

的其余部分
CREATE proc [dbo].[con]
@J_Sec as nvarchar(255),
@ged as varchar(max) 
as
declare @sql as varchar(max)

set @sql='
select 
(case when c.Age_18_24=1 then ''18-24'' when c.Age_25_29=1 then ''25-29'' when c.Age_30_39=1 then ''30-39'' 
 when c.Age_40_Above=1 then ''40-above'' else null  end)AS "Age",c.status 

from consumer c 
inner join dbo.Journey j on c.JOURNEY_SEQUENCE=j.JOURNEY_SEQUENCE
inner join Teams t on j.Team_id=t.Team_id where c.journey_sequence= '+@J_Sec+' and '+@ged;

exec(@sql)
go

2 个答案:

答案 0 :(得分:0)

如果您的@ged参数等于您在评论中所说的内容,那么为什么不放弃sql字符串方法并使用:

编辑以获取额外参数

CREATE proc [dbo].[con]
@J_Sec as nvarchar(255),
@male int,
@age_18_24 int,
@student int,
@main_lmg int,
@main_Price int,
@alt_lmg int,
@alt_price int,
@source_ka INT

as

select 
    (case 
        when c.Age_18_24=1 then '18-24' 
        when c.Age_25_29=1 then '25-29'
        when c.Age_30_39=1 then '30-39' 
        when c.Age_40_Above=1 then '40-above' 
        else null  
    end)
    AS "Age"
    , c.status 

from consumer c 
    inner join dbo.Journey j on c.JOURNEY_SEQUENCE = j.JOURNEY_SEQUENCE
    inner join Teams t on j.Team_id = t.Team_id 

where 
    c.journey_sequence= @J_Sec 
    and male != @male 
    and Age_18_24 != @age_18_24 
    and Student != @student 
    and Main_LMG != @main_lmg 
    and Main_Price != @main_Price 
    and ALT_LMG != @alt_lmg
    and ALT_Price != @alt_price 
    and Source_Ka != @source_ka

go

或者您可以尝试使用exec sp_executesql

答案 1 :(得分:0)

您可能会发现Crystal Reports会嗅探SP的输出签名。因为它没有一个(因为它隐藏在@sql字符串中),Crystal没有显示任何内容。

然而,这是一个猜测,因为我多年没有使用过Crystal。您可以通过绑定到此SP并检查是否获得任何结果来测试它...

CREATE PROCEDURE [dbo].[test] @mode AS INT
AS

DECLARE
  @sql AS VARCHAR(MAX)

IF (@mode = 1)
  SET @sql = 'SELECT ''This is mode one'' AS message'
ELSE
  SET @sql = 'SELECT ''You may only specify mode one'' AS error'

EXEC(@sql)

GO

如果你从中得不到任何东西,你似乎别无选择,只能以更传统的方式重构你的设计;像处理对象中的方法一样处理SP - 输入签名采用值,而不是代码,并具有固定的输出签名。

相关问题