报告时出现SQL查询问题

2022-03-15 00:00:00 sql sql-server-2008 sql-server ssrs-2008
我有一个SQL查询,它一切正常,但是我想使用一个参数(@geed)来解析‘where part’ 当我在SQL中尝试它时,它工作得很好..但是我不能让它在Crystal Report或SQL Reporting服务中工作 在此查询中..@J_Sec是一个参数,@Ged是WHERE语句的睡觉

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

sql

如果您的@geed参数与您的注释中所说的相同,那么为什么不放弃推荐答案字符串方法而使用:

编辑以获取额外参数

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

或者,您也可以尝试使用execsp_executesql。

相关文章