alter proc [dbo].[Pd_TB_Profit_DP]
(@FDateB datetime,@FDateE datetime,@ssql varchar(8000))
as
set nocount on
select convert(varchar(10),[FDate],120) as FDate ,
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
union all
select '合计',sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
这是我写的一个存储过程
两个select 用union连接起来的 如果 没有 后面的where 语句 查询出来的数据是这样的
FDate FTQty FTprofit FSQty FSprofit FZQty FZprofit
xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx
合计 xxx xxx xxx xxx xxx xxx
但是我加上where语句之后就这能查询出来
FDate FTQty FTprofit FSQty FSprofit FZQty FZprofit
合计 xxx xxx xxx xxx xxx xxx
就是第一个select语句 没有查到数据
这是怎么回事 然后 case when 是根据 FTYpe来分类的 一个总数据 FQTY Fprofit Ftyp=2的 为 Ftqty Ftprofit 这个没写错吧
如果还需要什么数据 我后面在补
(@FDateB datetime,@FDateE datetime,@ssql varchar(8000))
as
set nocount on
select convert(varchar(10),[FDate],120) as FDate ,
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
union all
select '合计',sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
这是我写的一个存储过程
两个select 用union连接起来的 如果 没有 后面的where 语句 查询出来的数据是这样的
FDate FTQty FTprofit FSQty FSprofit FZQty FZprofit
xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx
xxx xxx xxx xxx xxx xxx xxx
合计 xxx xxx xxx xxx xxx xxx
但是我加上where语句之后就这能查询出来
FDate FTQty FTprofit FSQty FSprofit FZQty FZprofit
合计 xxx xxx xxx xxx xxx xxx
就是第一个select语句 没有查到数据
这是怎么回事 然后 case when 是根据 FTYpe来分类的 一个总数据 FQTY Fprofit Ftyp=2的 为 Ftqty Ftprofit 这个没写错吧
如果还需要什么数据 我后面在补
*
from
(
select convert(varchar(10),[FDate],120) as FDate ,
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
union all
select '合计',sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid )t where FDate>=@FDateE and FDate<=@FDateB
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
单独执行该段语句能否查到值?
或者把存储过程中的where条件放到having字句中试试。
select convert(varchar(10),[FDate],120) as FDate ,
sum(case when t2.Ftype=2 then t1.FQty end) FTQty,
sum(case when t2.FType=2 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee)end) FTprofit,
sum(case when t2.Ftype=1 then t1.FQty end) FSQty,
sum(case when t2.FType=1 then (t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) end) FSprofit,
sum(t1.FQty) FZQty,sum(t1.FDAmount+t2.FDDHLFee-t1.Fcost-t2.FDHLFee) FZprofit
from SU_Sale_TBStockBillEntry t1 inner join SU_Sale_TBStockBill t2 on t1.fid=t2.fid where FDate>=@FDateE and FDate<=@FDateB
GROUP BY convert(varchar(10),[FDate],120)
从字符串向 datetime 转换时失败。 能否说下思路 错误
但是我用 [Pd_TB_Profit_DP] '2011-01-01 11:11:11.000','2011-11-11 11:11:11.000','' 这样执行存储过程的时候 就只有 一个 合计 然后就是null null 出现了
参考如下:
--动态sql语句基本语法
1 :普通SQL语句可以用Exec执行 eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名 declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错 declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确 3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中? declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num