问题1:
我要实现的一个存储过程需要接收一个这样的参数:
@PrjList varchar(1000)
正常情况下,参数保存的应该是类似下面的格式:
'项目1,项目2,项目3,项目5'这个参数在是在WHERE条件里被使用的,如:
WHERE 项目 IN (@belongProjcet)即,在外面决定查询的范围,可能属于一个项目,也可能属于多个项目,动态组合成字符串传进来。我遇到的问题是,如果传入的是 '项目1,项目2,项目3'格式的字符串,执行存储过程后,根本查不出数据,应该是逗号的问题:
即我最终需要的是:WHERE 项目 IN ('项目1','项目2','项目3')
而象我那么做得到的却是:WHERE 项目 IN ('项目1,项目2,项目3')所以查不到东西啊,大家给看看这个参数怎么组合能得到需要的结果。
问题2:
写存储过程的时候,遇到这样的问题,例如
Select A,
       Count(*),--合计
       (Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
       (Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
       (Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
        [X]-[Y],--这列需要两个子查询的差
        [X]-[Y]/[Z],这列是多个子查询的更复杂的表达式
From Test t
Group by A我的问题是:可不可以声明一些变量,在第一次求得[X][Y][Z]值的时候,就把值赋给这些变量,然后在后边需要[X][Y][Z]组合计算的时候,就不需要再重新写一遍相同的子查询,不仅效率低,看着也乱。
类似这样:
Declare X,Y,Z
Select A,
       Count(*),--合计
       X=(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
       Y=(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
       Z=(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
        X-Y,--这列使用记录了子查询结果的变量来计算
        X-Y/Z,--这列使用记录了子查询结果的变量来计算
From Test t
Group by A我这样实验了,但是没有成功,可能是语法或别的什么地方不对,大家给看看吧。

解决方案 »

  1.   

    to xyxfly(小虾米):
       嘿,最近做报表,比较郁闷
      

  2.   

    问题1
    replace(''''+@PrjList+'''',',',''',''')
    可以把 
    项目1,项目2,项目3,项目5 
    变成 
    '项目1','项目2','项目3','项目5'
      

  3.   

    问题2:
    先这样简化
    Select A,
           Count(*),--合计
           sum(case when xxx='某值' then 1 else 0 end) as [X],
           sum(case when yyy='某值' then 1 else 0 end) as [Y],
           sum(case when zzz='某值' then 1 else 0 end) as [Z],
           sum(case when xxx='某值' then 1 else 0 end)-
           sum(case when yyy='某值' then 1 else 0 end) as [Y-Y],
           sum(case when xxx='某值' then 1 else 0 end)-
           sum(case when yyy='某值' then 1 else 0 end)/
           sum(case when zzz='某值' then 1 else 0 end) as [Y-Y/Z]
    From Test t
    Group by A
      

  4.   

    问题1
    比较全的写法
    declare @sql varchar(8000)
    set @sql='select * from test where 项目 IN ('+replace(''''+@PrjList+'''',',',''',''')+')'print @sql
    exec(@sql)问题2
    用变量是不可能的
    我的写法看起来好像很好理解(沾沾自喜中.....)
      

  5.   

    to tx1icenhe(冒牌马可 V0.3) :
       就是某组 sum(case when xxx='某值' then 1 else 0 end) 语句,要反复出现多次了?
      

  6.   

    Declare X,Y,Z
    Select A,
           Count(*),--合计
           X=(Select count(*) from test where xxx='某值' and A=t.A),--分组统计的合计值[X]
           Y=(Select count(*) from test where yyy='某值' and A=t.A),--分组统计的合计值[Y]
           Z=(Select count(*) from test where zzz='某值' and A=t.A),--分组统计的合计值[Z]
            X-Y,--这列使用记录了子查询结果的变量来计算
            X-Y/Z,--这列使用记录了子查询结果的变量来计算
    From Test t
    Group by A
    =========
    这样写:
    Select B.A ,
           B.Cnt,
           B.X,B.Y,B.Z, B.X- B.Y , B.X - B.Y /B.Z
    From
    (
    Select A,
           Count(*) As Cnt
           X=(Select count(*) from test where xxx='某值' and A=t.A),
           Y=(Select count(*) from test where yyy='某值' and A=t.A),
           Z=(Select count(*) from test where zzz='某值' and A=t.A)
    From Test t
    Group by A
    ) B;)
      

  7.   

    问题1:
    WHERE CHARINDEX(项目,@belongProjcet)>0
      

  8.   

    progra(不学无数又富于幻想的人,虽有翅膀却无双脚) ( ) 信誉:100  2006-08-09 14:12:00  得分: 0  
     
     
       to tx1icenhe(冒牌马可 V0.3) :
       就是某组 sum(case when xxx='某值' then 1 else 0 end) 语句,要反复出现多次了?
      
     
    ---------------------------------------------------------
    不是子查询出现多次不怕的,对性能影响不大
      

  9.   

    create table tt
    (
    项目 varchar(20),
    资金 int
    )
    insert into tt
    select '项目1',1000 union all
    select '项目2',1000 union all
    select '项目3',1000 union all
    select '项目4',1000select * from tt
    declare @PrjList varchar(800),
            @temp varchar(500)
    set @PrjList = '项目1,项目2,项目3'
    set @temp = '('''+replace(@PrjList,',',''',''')+''')'
    print @temp
    exec('select * from tt where 项目 in '+@temp)create table tt
    (
    xxx varchar(10),
    yyy varchar(10),
    zzz varchar(10)
    )
    insert into tt
    select 'a','aa','aaa' union all
    select 'b','bb','bbb' union all
    select 'c','cc','ccc' union all
    select 'c','dd','ccc'
    select * from tt-用临时表
    select X=(select count(*) from tt where xxx='c'),
           Y=(select count(*) from tt where xxx='b'),
           Z=(select count(*) from tt where yyy='bb')
    into #t from tt
    select X,Y,Z,X-Y as [X-Y],X-Y/Z as [X-Y/Z] from #t
    DROP TABLE #T
      

  10.   

    使用 tx1icenhe(冒牌马可 V0.3) 的replace方法倒是可以把字符串格式化为正确的格式,但是我把格式化后的字符串放到 Where ... AND 项目 in (@Project) 的语句里,执行后却什么都查不出来,而用 Where ... AND 项目 in ('项目1','项目2','项目3') 的方式却能查出来,怎么回事啊!?
      

  11.   

    用动态SQL语句
    declare @PrjList varchar(800),
            @temp varchar(500)
    set @PrjList = '项目1,项目2,项目3'
    set @temp = '('''+replace(@PrjList,',',''',''')+''')'
    print @temp
    exec('select * from tt where 项目 in '+@temp)
    这可以查出来呀
      

  12.   

    对于Column in('a','b','c')这种情况可以不使用动态查询,只要使用charindex()函数就可以.例如:(注意:必须是英文逗号,不能是中文逗号)
    declare @t table(x varchar(20),id int identity(1,1))
    insert @t
    select '项目1' union all
    select '项目2' union all
    select '项目3' union all
    select '项目4' union all
    select '项目5' union all
    select '项目6'declare @Prjlist varchar(100)
    set @Prjlist = '项目1,项目2,项目3,项目5'
    select * from @t where charindex(','+ x + ',',',' + @Prjlist + ',') > 0
    --注释:
    --在x列以及@Prjlist前后各加一个逗号,使二者[格式]完全相同,即类似:
    --charindex(',项目1,',',项目1,项目2,项目3,项目5,')
      

  13.   

    同意   zjdyzwx(十一月猪) 的方法个人觉得比较好---------------------------------------------------
    我的网站:http://www.obai.net 卖首饰的
    http://www.booksoo.com  卖书的
      

  14.   

    to:xyxfly(小虾米 -_- 何去何从) 
    多谢关注.分先攒着,等到二个星再说.^^
      

  15.   

    MS SQL sever的过程没弄过,都是用PGSQL的
      

  16.   

    hellowork(一两清风) ( ) 信誉:100  2006-08-10 00:00:00  得分: 0  
     
     
       to:xyxfly(小虾米 -_- 何去何从) 
    多谢关注.分先攒着,等到二个星再说.^^
      
     
    到时多给点啊,先预定了  ^_^     0_0
      

  17.   

    declare @prjList varchar(800),
            @str varchar(500)
    set @prjList = '项目1,项目2,项目3'
    set @str = '('''+replace(@prjList,',',''',''')+''')'
    exec('select * from 表 where 项目 in '+@str )