您的位置:程序门 -> ms-sql server -> 基础类



存储过程的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


存储过程的问题
发表于:2007-09-05 10:40:57 楼主
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm,
sum(case   kz   when   'p '   then   fks   else   0   end)+
sum(case   kz   when   'z '   then   fks   else   0   end)+
sum(case   kz   when   'f '   then   fks   else   0   end)-
sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs
from   k2007
group   by   jgm
having   @jgm=jgm


use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   *   from   #a
(select   jgm,
sum(case   kz   when   'p '   then   fks   else   0   end)+
sum(case   kz   when   'z '   then   fks   else   0   end)+
sum(case   kz   when   'f '   then   fks   else   0   end)-
sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs
into   #a
from   k2007
group   by   jgm)
where     @jgm=jgm

麻烦哪位大虾帮我看下,错在哪里,第一种写法查询出来的是空值,第二种基本不行,在线等!第一次写存储过程,别见笑!
发表于:2007-09-05 10:44:231楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm   ,   p+z+f-fkzs   from

    select   jgm,
    sum(case   kz   when   'p '   then   fks   else   0   end)   ,   p
    sum(case   kz   when   'z '   then   fks   else   0   end)   ,   z
    sum(case   kz   when   'f '   then   fks   else   0   end)   ,   f
    sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs
    from   k2007
    group   by   jgm
    having   @jgm=jgm
)   t
发表于:2007-09-05 10:46:232楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
as
select   jgm   ,   p+z+f-fkzs   from

    select   jgm,
    sum(case   kz   when   'p '   then   fks   else   0   end)   ,   p
    sum(case   kz   when   'z '   then   fks   else   0   end)   ,   z
    sum(case   kz   when   'f '   then   fks   else   0   end)   ,   f
    sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs
    from   k2007
    group   by   jgm
)   t
发表于:2007-09-05 10:46:463楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm,
sum(case   kz   when   'p '   then   1
                        when   'z '   then   1  
                        when   'f '   then   1
                        when   'x '   then   -1   end   *fks)   as   fkzs
from   k2007
group   by   jgm
having   @jgm=jgm
发表于:2007-09-05 10:47:214楼 得分:0
不行啊,潇洒
发表于:2007-09-05 10:47:395楼 得分:0
1.   from   k2007   where   jgm=@jgm
2.   select   *   from   #a   where   jgm=@jgm
发表于:2007-09-05 10:49:186楼 得分:0
001的也不行
发表于:2007-09-05 10:49:277楼 得分:0
你是只查一个jgm?

use   ksj
go
create   proc   fkzscc--发卡总量存储
as
select   jgm   ,   p+z+f-fkzs   from

    select   jgm,
    sum(case   kz   when   'p '   then   fks   else   0   end)   ,   p
    sum(case   kz   when   'z '   then   fks   else   0   end)   ,   z
    sum(case   kz   when   'f '   then   fks   else   0   end)   ,   f
    sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs
    from   k2007
    where   jgm   =   @jgm
    group   by   jgm
)   t
发表于:2007-09-05 10:50:288楼 得分:0
我的意思是先建一个临时表,再从临时表里读数据,这样可能好点,但是我就不知道具体的写法,麻烦谁指点下!
发表于:2007-09-05 10:50:479楼 得分:0
查询一个jmg下面的发卡量
发表于:2007-09-05 10:53:0110楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
    select   jgm=@jgm   ,   t1.fks   -   t2.fks   from
    (select   sum(fks)   fks   from   k2007   where   jgm   =   @jgm   and   (kz   =   'p '   or   kz   =   'z '   or   kz   =   'f '))   t1,
    (select   sum(fks)   fks   from   k2007   where   jgm   =   @jgm   and   (kz   =   'x '))   t2
发表于:2007-09-05 10:54:4011楼 得分:0
上面这个肯定行.
发表于:2007-09-05 10:58:1212楼 得分:0
jgm                                     jgmc                           sl         fkrq                                     kz
001                                     昆明11                         22   2007-01-01   00:00:00.000     p
这就是k2007表中的格式,靠kz来区分卡的种类,这张表中有p,z,f三种卡,x是销卡,统计时要p+z+f-x才是数量,我建这个存储是只要输入jgm就可以找出这个机构下的发卡量
发表于:2007-09-05 11:01:5113楼 得分:0
我自己想复杂了,晕
发表于:2007-09-05 11:04:4614楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm,
sum(fks)   as   fkzs
from   k2007
group   by   jgm
having   @jgm=jgm
但是搞成这样也不行啊,晕
发表于:2007-09-05 11:07:1815楼 得分:0
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm,
sum(case   kz   when   'p '   then   1
                        when   'z '   then   1  
                        when   'f '   then   1
                        when   'x '   then   -1   end   *fks)   as   fkzs
from   k2007   where   jgm   =   @jgm
group   by   jgm

就是這個阿
发表于:2007-09-05 11:11:4716楼 得分:0
fks   應該是   s1     吧   ,沒有fks   這個字段啊
use   ksj
go
create   proc   fkzscc--发卡总量存储
@jgm   char(10)
as
select   jgm,
sum(case   kz   when   'p '   then   1
                        when   'z '   then   1  
                        when   'f '   then   1
                        when   'x '   then   -1   end   *fks)   as   fkzs
from   k2007   where   jgm   =   @jgm
group   by   jgm

发表于:2007-09-05 11:17:2117楼 得分:0
create   table   #t   (jgm   varchar(1),fks   int,   kz   varchar(1))
insert   #t
select   '1 ',5, 'p '   union   all
select   '1 ',6, 'z '   union   all
select   '1 ',5, 'f '   union   all
select   '1 ',1, 'x '

測試數據
select   jgm,
sum(case   kz   when   'p '   then   1
                        when   'z '   then   1  
                        when   'f '   then   1
                        when   'x '   then   -1   end   *fks)   as   fkzs
from   #t   where   jgm   =   '1 '
group   by   jgm

drop   table   #t
--------------------------
jgm     fkzs                
----   -----------  
1         15
发表于:2007-09-05 14:31:3418楼 得分:0
不行啊,出来的是空值
发表于:2007-09-05 15:25:4619楼 得分:0
use   ksj
go
select   jgm,
sum(case   kz   when   'p '   then   fks   else   0   end)+
sum(case   kz   when   'z '   then   fks   else   0   end)+
sum(case   kz   when   'f '   then   fks   else   0   end)-
sum(case   kz   when   'x '   then   fks   else   0   end)   as   fkzs   into   #a
from   k2007  
group   by   jgm
create   proc   fkzscc--发卡总数查询
@jgm   char
as
select   *   from   #a
where   @jgm=jgm


这样也不行
消息   111,级别   15,状态   1,过程   fkzscc,第   12   行
'create/alter   procedure '   必须是查询批次中的第一个语句。


快速检索

最新资讯
热门点击