其中and czbt2.hnxc.czbt_btdj.lxdm in('0501','0502','0503')和
and month(djrq)=3 
两个条件为不确定条件是指可以为:czbt2.hnxc.czbt_btdj.lxdm in('0541','0542','0513')
and month(djrq)=11

解决方案 »

  1.   

    结果要求导出为一个文本文件,比如3month.txt
      

  2.   

    create procedure sp_test
    @lxdm  varchar(40),
    @month int
    as
    begin
        declare @sql varchar(8000)
        
        set @sql='select cast(yhzh as char(20))+cast(object_name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(idcard_num  as char(20))+cast(sum(btje) as char(16))+'                    ' 
        from czbt2.hnxc.czbt_btdj,t_sn_object,t_sn_object_zhxx 
        where t_sn_object.id=t_sn_object_zhxx.btdx_uid 
        and t_sn_object.id=czbt2.hnxc.czbt_btdj.btdx_uid 
        and czbt2.hnxc.czbt_btdj.lxdm in('+@lxdm+') 
        and month(djrq)='+rtrim(@month)+' 
        group by yhzh,object_name,gh,idcard_num 
        order by gh'    exec(@sql)
    end
    goexec sp_test '''0501'',''0502'',''0503''',3
    exec sp_test '''0541'',''0542'',''0513''',11
    go
      

  3.   

    create procedure sp_test
    @lxdm  varchar(40),
    @month int
    as
    begin
        declare @sql varchar(8000)
        
        set @sql='select cast(yhzh as char(20))+cast(object_name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(idcard_num  as char(20))+cast(sum(btje) as char(16))+''                    '' 
        from czbt2.hnxc.czbt_btdj,t_sn_object,t_sn_object_zhxx 
        where t_sn_object.id=t_sn_object_zhxx.btdx_uid 
        and t_sn_object.id=czbt2.hnxc.czbt_btdj.btdx_uid 
        and czbt2.hnxc.czbt_btdj.lxdm in('+@lxdm+') 
        and month(djrq)='+rtrim(@month)+' 
        group by yhzh,object_name,gh,idcard_num 
        order by gh'    exec(@sql)
    end
    goexec sp_test '''0501'',''0502'',''0503''',3
    exec sp_test '''0541'',''0542'',''0513''',11
    go
      

  4.   

    create proc test_p @lxdm varchar(1000),@month int
    asselect cast(yhzh as char(20))+cast(object_name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(idcard_num  as char(20))+cast(sum(btje) as char(16))+'                    ' 
    from czbt2.hnxc.czbt_btdj,t_sn_object,t_sn_object_zhxx 
    where t_sn_object.id=t_sn_object_zhxx.btdx_uid 
    and t_sn_object.id=czbt2.hnxc.czbt_btdj.btdx_uid 
    and  charindex(','+czbt2.hnxc.czbt_btdj.lxdm+',',','+@lxdm+',')>0
    and month(djrq)=@month
    group by yhzh,object_name,gh,idcard_num 
    order by gh
      

  5.   

    简化一下上面的语句为:select cast(zh as char(20))+cast(name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(sfz  as char(20))+cast(sum(je) as char(16))+'                    ' 
    from tba,tbb,tbc 
    where tba.id=tbb.uid 
    and tba.id=tbc.uid 
    and tbc.lxdm in('0501','0502','0503') 
    and month(djrq)=3 
    group by zh,name,gh,sfz 
    order by gh
      

  6.   

    Create ProceDure SP_TEST(@lxdm Varchar(100), @djrq Int)
    As
    Begin
    select cast(yhzh as char(20))+cast(object_name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(idcard_num  as char(20))+cast(sum(btje) as char(16))+'                    ' 
    from czbt2.hnxc.czbt_btdj,t_sn_object,t_sn_object_zhxx 
    where t_sn_object.id=t_sn_object_zhxx.btdx_uid 
    and t_sn_object.id=czbt2.hnxc.czbt_btdj.btdx_uid 
    and CharIndex(czbt2.hnxc.czbt_btdj.lxdm, @lxdm) > 0
    and month(djrq)=@djrq 
    group by yhzh,object_name,gh,idcard_num 
    order by gh
    End
      

  7.   

    可以不用動態SQL語句,用CharIndex實現,另外,編碼似乎有一定的規律,那麼組合的時候就可以不用在前後加","了。
      

  8.   

    提示错误170 第8行 
    from czbt2.hnxc.czbt_btdj,t_sn_object,t_sn_object_zhxx 
        where t_sn_object.id=t_sn_object_zhxx.btdx_uid 
        and t_sn 附近为语法错误
      

  9.   

    以上偶的存储过程调用方法
    exec '0541,0542,0513' ,3
      

  10.   

    roy_88(中国风_燃烧你的激情!!!) ( ) 信誉:100    Blog   加为好友  2007-04-18 17:03:54  得分: 0  
     
     
       以上偶的存储过程调用方法
    exec '0541,0542,0513' ,3
      
     
    --------------:),掉了存儲過程名稱.
      

  11.   

    我的這麼調用 EXEC  SP_TEST '0541,0542,0513' ,3
    roy_88的這麼調用 EXEC  test_p '0541,0542,0513' ,3
      

  12.   

    谢谢paoluo(一天到晚游泳的鱼)
    谢谢roy_88(中国风_燃烧你的激情!!!) 能否将结果导出为一个文本文件?
      

  13.   

    建立了存儲過程後,Exec master..xp_cmdshell 'bcp "EXEC DataBaseName.dbo.SP_TEST ''0541,0542,0513'' ,3" Queryout "D:\TEST.txt" -c -q -S "ServerName" -U "User" -P "PW"'將DataBaseName ServerName User PW分別替換為你數據庫對應的實際參數
      

  14.   

    TO :hwhtj(爱情灾民)libin_ftsafe(子陌红尘) 的不好吗??