+ -
当前位置:首页 → 问答吧 → 存储过程,怎么让一个变量来当作查询条件来用?

存储过程,怎么让一个变量来当作查询条件来用?

时间:2011-12-09

来源:互联网

ALTER PROCEDURE [dbo].[proc_business]
@user varchar(20),
@city varchar(20)
AS

BEGIN
declare @wheretrue varchar(100)
if(@city='') set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+@user+''''
else set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+''''

select * from P_BUSINESS_LIST where DEPT_CODE in (@wheretrue)


print @wheretrue
END
我要把@wheretrue这个变量当作下面的SQL的查询条件。
不知道为什么查不出数据,SQL单独拉出来执行就可以,在存储过程里面就不行。可能是把@wheretrue这个变量当作字符串来处理了。求各位帮忙解决一下。

作者: HuangKJSay   发布时间: 2011-12-09

动态语句啊。

作者: fredrickhu   发布时间: 2011-12-09

SQL code
ALTER PROCEDURE [dbo].[proc_business]
(
@user varchar(20),
@city varchar(20)
)
AS

BEGIN
declare @wheretrue varchar(100)
if(@city='') 
set @wheretrue='select DEPT_CODE from T_USERDEPT where USER_NO='''+ltrim(@user)+''''
else
set @wheretrue ='select DEPT_CODE from T_DEPT where BRANCH_CODE='''+@city+''''
exec('select * from P_BUSINESS_LIST where DEPT_CODE in (' +@wheretrue+ ')')
END

作者: fredrickhu   发布时间: 2011-12-09

引用 2 楼 fredrickhu 的回复:
SQL code

ALTER PROCEDURE [dbo].[proc_business]
(
@user varchar(20),
@city varchar(20)
)
AS

BEGIN
declare @wheretrue varchar(100)
if(@city='')
set @wheretrue='select DEPT_CODE from T_USE……

恩,这样是可以解决。但不知还有没有别的办法呢?我没想到会是这种方法,所以没有把整个存储过程粘贴出来。
如果是更复杂的,这样好像行不通吧。就算行得通应该非常的麻烦
下面是我其中的一个存储过程。要把上面那个条件加上去,这样还行得通吗?

作者: HuangKJSay   发布时间: 2011-12-09

ALTER PROCEDURE [dbo].[order_list]
@city varchar(20),
@stat_time varchar(20),
@end_time varchar(20),
@dept_code varchar(20),
@zuqi int
AS
BEGIN
create table #order(
stat_time varchar(100),
end_time varchar(100),
dept_code varchar(100),
zuqi int,
--type_id int, --车型ID
type_name varchar(100), --车型名称
type_car_num numeric(18,0), --车型车辆数量
cz_day int, --出租天数
cz_ci int, --出租次数
cz_or_day numeric(18,2), --出租单/天数
no_day numeric(18,2), --单台出租天数
cz_percent numeric(18,2),--出租率
zj numeric(18,2), --租金收入
cj numeric(18,2), --车均收入

type_car_num2 numeric(18,0), --车型车辆总数量
cz_day2 numeric(18,0), --出租总天数
cz_ci2 numeric(18,0), --出租总次数
cz_or_day2 numeric(18,2),--总出租单/天数
no_day2 numeric(18,2), --总单台出租天数
cz_percent2 numeric(18,2),--总出租率
zj2 numeric(18,2), --总租金收入
cj2 numeric(18,2) --总车均收入
)

create table #order_time(
order_no varchar(100), --出租单号
type_id int, --车型ID
s_time datetime, --起算时间
end_time datetime, --结束时间
JFTX int, --计费天数
day_num int, --该单计算天数
day_input int, --输入计算天数
order_money numeric(18,2), --租金
state_ind int, --状态
dept_codein varchar(100) --所属门店
)
SET NOCOUNT ON;


--select * from P_CAR
--select * from P_CAR_TYPE
--select * from P_CAR_ORDER where (STATE_IND='0' or STATE_IND='3' or STATE_IND='2')
--select * from T_dept where sign='P'

--select * from t_dept

insert #order_time(order_no,type_id,s_time,end_time,state_ind,dept_codein)
select order_no, car_type, case when datediff(day,S_TIME,@stat_time)=0 then S_TIME
when datediff(day,S_TIME,@stat_time)<0 then S_TIME
when datediff(day,S_TIME,@stat_time)>0 then @stat_time end,
case when datediff(day,E_TIME_AFF,@end_time)=0 then E_TIME_AFF
when @end_time='' then E_TIME_AFF
when datediff(day,E_TIME_AFF,@end_time)<0 then @end_time+' 23:59'
when datediff(day,E_TIME_AFF,@end_time)>0 then E_TIME_AFF end,STATE_IND,DEPT_CODEIN
from p_CAR_ORDER where (STATE_IND=0 or STATE_IND=2) and (@end_time = '' or S_TIME<@end_time+' 23:59') and (@stat_time='' or E_TIME_AFF>@stat_time) and (@dept_code='' or DEPT_CODEIN=@dept_code) and (@zuqi=0 or ZUQI<@zuqi) and (@city='' or DEPT_CODE in (select DEPT_CODE from T_DEPT where BRANCH_CODE=@city))



insert #order_time(order_no,type_id,s_time,end_time,state_ind,dept_codein)
select order_no, car_type, case when datediff(day,S_TIME,@stat_time)=0 then S_TIME
when datediff(day,S_TIME,@stat_time)<0 then S_TIME
when datediff(day,S_TIME,@stat_time)>0 then @stat_time end,
case when datediff(day,yjhc_time,@end_time)=0 then yjhc_time
when @end_time='' then yjhc_time
when datediff(day,yjhc_time,@end_time)<0 then @end_time+' 23:59'
when datediff(day,yjhc_time,@end_time)>0 then yjhc_time end,STATE_IND,dept_codein
from p_CAR_ORDER where STATE_IND=3 and (@end_time='' or S_TIME<@end_time+' 23:59') and (@stat_time='' or convert(datetime,yjhc_time)>@stat_time) and (@dept_code='' or DEPT_CODEIN=@dept_code) and (@zuqi=0 or ZUQI<@zuqi) and (@city='' or DEPT_CODE in (select DEPT_CODE from T_DEPT where BRANCH_CODE=@city))


update #order_time set day_num=(case when (datediff(hour,s_time,end_time)-datediff(day,s_time,end_time)*24)>=4 then datediff(day,s_time,end_time)+1
when (datediff(hour,s_time,end_time)-datediff(day,s_time,end_time)*24)=0 then datediff(day,s_time,end_time)
when (datediff(hour,s_time,end_time)-datediff(day,s_time,end_time)*24)<4 then datediff(day,s_time,end_time)
end)

update #order_time set day_input=(datediff(day,@stat_time,@end_time)+1)

update ord set order_money=(select total_amount from P_CAR_ORDER where STATE_IND=0 and order_no=ord.order_no) from #order_time ord

update ord set JFTX=(select JFTX from P_CAR_ORDER where STATE_IND=0 and order_no=ord.order_no) from #order_time ord
--select * from #order_time

--select * from P_CAR_ORDER
--select * from #order_time
--select * from #order_time
insert into #order(type_name,type_car_num,cz_day,cz_ci,cz_or_day,no_day,cz_percent,zj,cj)
select TYPE,(select count(*) from P_CAR where type=p_type.ID),--车型,车辆数量

(select sum(day_num) from #order_time where type_id=p_type.ID),--出租天数

--(select count(*) from #order_time where type_id=p_type.ID), --出租次数
(select COUNT(*) from P_CAR_ORDER where S_TIME>@stat_time and S_TIME<@end_time+' 23:59' and (@dept_code='' or DEPT_CODEIN=@dept_code) and (@zuqi=0 or ZUQI<@zuqi) and (@city='' or DEPT_CODE in (select DEPT_CODE from T_DEPT where BRANCH_CODE=@city)) and CAR_TYPE=p_type.ID),
case convert(numeric(18,2),(select count(*) from #order_time where type_id=p_type.ID)) when 0 then 0 else convert(numeric(18,2),(select sum(day_num) from #order_time where type_id=p_type.ID))/convert(numeric(18,2),(select count(*) from #order_time where type_id=p_type.ID)) end,--出租单/天数

case convert(numeric(18,2),(select count(*) from P_CAR where type=p_type.ID)) when 0 then 0 else convert(numeric(18,2),(select sum(day_num) from #order_time where type_id=p_type.ID))/convert(numeric(18,2),(select count(*) from P_CAR where type=p_type.ID)) end, --单台出租天数

case convert(numeric(18,2),(select count(*) from P_CAR where type=p_type.ID)) when 0 then 0 else case (select top 1 day_input from #order_time) when 0 then 0 else (case convert(numeric(18,2),(select count(*) from P_CAR where type=p_type.ID)) when 0 then 0 else convert(numeric(18,2),(select sum(day_num) from #order_time where type_id=p_type.ID))/convert(numeric(18,2),(select count(*) from P_CAR where type=p_type.ID)) end)/(select top 1 day_input from #order_time) end end, --出租率

(select sum(case JFTX when 0 then 0 else order_money/JFTX*day_num end) from #order_time where state_ind=0 and type_id=p_type.ID), --租金收入

case (select sum(day_num) from #order_time where type_id=p_type.ID) when 0 then 0 else (select sum( case JFTX when 0 then 0 else order_money/JFTX*day_num end) from #order_time where type_id=p_type.ID)/(select sum(day_num) from #order_time where type_id=p_type.ID) end --车均收入

from P_CAR_TYPE p_type

--汇总

update #order set type_car_num2=(select sum(type_car_num) from #order) --车数

update #order set cz_day2=(select sum(cz_day) from #order) --出租天数

update #order set cz_ci2=(select sum(cz_ci) from #order) --出租次数

update #order set cz_or_day2=(select top 1 case cz_ci2 when 0 then 0 else cz_day2/cz_ci2 end from #order) --出租单/天数

update #order set no_day2=(select top 1 cz_day2/type_car_num2 from #order) --单台出租天数

update #order set cz_percent2=(no_day2/(select top 1 day_input from #order_time)) --出租率

update #order set zj2=(select sum(zj) from #order) --租金收入

update #order set cj2=(zj2/cz_day2)

update #order set stat_time=@stat_time

update #order set end_time=@end_time

update #order set zuqi=@zuqi

update #order set dept_code=(select DEPT_NAME from T_DEPT where DEPT_CODE=@dept_code)
select * from #order



--select * from #order
--select datediff(hour,'2011-09-30 19:04:00.000','2011-10-07 23:59:00.000')-datediff(day,'2011-09-30 19:04:00.000','2011-10-07 23:59:00.000')*24/24+24
--select 7*24
--select * from P_CAR_ORDER where order_no='Z2011090007'
--select datediff(day,'2011-05-01','2011-09-10')


END


--exec order_list 'HGZL002','2011-10-15','2011-10-15','HGSZ0005',0

作者: HuangKJSay   发布时间: 2011-12-09

另一种写法,
SQL code

ALTER PROCEDURE [dbo].[proc_business]
(
@user varchar(20),
@city varchar(20)
)
AS
begin
 declare @wheretrue varchar(100)
 if(@city='') 
    select DEPT_CODE into #t from T_USERDEPT where USER_NO=ltrim(@user)
 else
    select DEPT_CODE into #t from T_DEPT where BRANCH_CODE=@city

select a.*
from P_BUSINESS_LIST a
inner join #t b
on a.DEPT_CODE=b.DEPT_CODE
end

作者: ap0405140   发布时间: 2011-12-09