报告时出现SQL查询问题

本教程将介绍报告时出现SQL查询问题的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

报告时出现SQL查询问题 教程 第1张

问题描述

我有一个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。

好了关于报告时出现SQL查询问题的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。