CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

存储过程问题!在线等!重谢!

楼主lqxty()2005-08-03 16:29:55 在 MS-SQL Server / 基础类 提问

建立一个存储过程  
  数据库名为:sqldb       表名为:table     日期条件为:table中的一个时间列大于2005-01-01小于2005-05-01  
  字符条件为:table表中的一个字符串列分别等于aaa或bbb或ccc或ddd或eee,每个字符条件也都必须符合上述时间范围  
  将每一个字符条件存为一个文件,例如:c:\aaa.xls,c:\bbb.xls,c:\ccc.xls,c:\ddd.xls,c:\eee.xls  
  也就是说,在2005-01-01小于2005-05-01这个时间范围内字符串列分别等于aaa或bbb或ccc或ddd或eee时的所以记录。  
  万分感谢!!! 问题点数:100、回复次数:10Top

1 楼zjcxc(邹建)回复于 2005-08-03 16:56:08 得分 50

 
  use   sqldb  
  go  
   
  --   存储过程  
  create   proc   p_export  
  @str   nvarchar(4000)  
  as  
  declare   @s   nvarchar(4000)  
  WHILE   CHARINDEX(',',@str)>0  
  BEGIN  
  select   @s='bcp   "select   *   from   sqldb..[table]   where   日期列>''2005-01-01''   and   日期列<''2005-01-01''  
  and   字符串列='''+replace(LEFT(@str,CHARINDEX(',',@str)-1),'''','''''')+'''"   queryout   "c:\'  
  +LEFT(@str,CHARINDEX(',',@str)-1)+'.xls"   /T   /c',  
  @str=STUFF(@str,1,CHARINDEX(',',@str),'')  
  exec   master..xp_cmdshell   @s,no_output  
  END  
  select   @s='bcp   "select   *   from   sqldb..[table]   where   日期列>''2005-01-01''   and   日期列<''2005-01-01''  
  and   字符串列='''+replace(@str,'''','''''')+'''"   queryout   "c:\'  
  +@str+'.xls"   /T   /c'  
  exec   master..xp_cmdshell   @s,no_output  
  Top

2 楼zjcxc(邹建)回复于 2005-08-03 16:58:57 得分 0

--不要换行  
   
  use   sqldb  
  go  
   
  --   存储过程  
  alter   proc   p_export  
  @str   nvarchar(4000)  
  as  
  declare   @s   nvarchar(4000)  
  WHILE   CHARINDEX(',',@str)>0  
  BEGIN  
  select   @s='bcp   "select   *   from   sqldb..[table]   where   日期列>''2005-01-01''   and   日期列<''2005-01-01''   and   字符串列='''+replace(LEFT(@str,CHARINDEX(',',@str)-1),'''','''''')+'''"   queryout   "c:\'  
  +LEFT(@str,CHARINDEX(',',@str)-1)+'.xls"   /T   /c',  
  @str=STUFF(@str,1,CHARINDEX(',',@str),'')  
  exec   master..xp_cmdshell   @s,no_output  
  END  
  select   @s='bcp   "select   *   from   sqldb..[table]   where   日期列>''2005-01-01''   and   日期列<''2005-01-01''   and   字符串列='''+replace(@str,'''','''''')+'''"   queryout   "c:\'  
  +@str+'.xls"   /T   /c'  
  exec   master..xp_cmdshell   @s,no_output  
  Top

3 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-08-03 17:04:04 得分 50

--生成测试数据  
  use   sqldb  
  go  
   
  create   table   table(name   varchar(20),dates   datetime)  
  insert   into   [table]   select   'aaa','2005-01-01'  
  insert   into   [table]   select   'bbb','2005-01-01'  
  insert   into   [table]   select   'ccc','2005-01-01'  
  insert   into   [table]   select   'ddd','2005-01-01'  
  insert   into   [table]   select   'eee','2005-01-01'  
  go  
   
   
  --创建存储过程  
  create   procedure   sp_bcp(@strdate   datetime,@enddate   datetime)  
  as  
  begin  
          declare   @name   varchar(20)  
          declare   t_cursor   cursor   for    
          select   distinct   name   from   t   where   dates   between   @strdate   and   @enddate  
           
          open   t_cursor  
           
          fetch   next   from   t_cursor   into   @name  
           
          while   @@fetch_status   =   0  
          begin  
                  exec   master..xp_cmdshell   'bcp   "select   *   from   sqldb.dbo.[table]   where   id='''+@name+'''   and   dates   between   '''+@strdate+'''   and   '''+@enddate+'''"   queryout   "C:\'+@name+'.xls"   -c   -q   -U"sa"   -P"xxx"',no_output  
                  fetch   next   from   t_cursor   into   @name  
          end  
           
          close   t_cursor  
          deallocate   t_cursor  
  end  
  go  
   
   
  --执行存储过程  
  exec   sp_bcp   '2005-01-01','2005-05-01'Top

4 楼surn(孤影.*)回复于 2005-08-03 17:05:41 得分 0

xuexiTop

5 楼phantomMan()回复于 2005-08-03 17:07:07 得分 0

create   table   table1(  
  id   int   identity,  
  datefield   datetime,  
  strfield   varchar(20)  
  )  
   
  insert   into   table1   values('2004-01-01','aaa')  
  insert   into   table1   values('2005-01-07','aaa')  
  insert   into   table1   values('2005-01-07','bbb')  
  insert   into   table1   values('2005-01-23','bbb')  
   
  select   *   from   table1  
   
  --导出数据:  
   
  exec   master..xp_cmdshell   'bcp   "select   *   from   ColinTest..table1   where   datefield>''2005-01-01''   and   datefield<''2005-05-01''   and   strfield=''aaa''   "   queryout   c:\aaa.xsl   -c'  
  exec   master..xp_cmdshell   'bcp   "select   *   from   ColinTest..table1   where   datefield>''2005-01-01''   and   datefield<''2005-05-01''   and   strfield=''bbb''   "   queryout   c:\bbb.xsl   -c'  
  exec   master..xp_cmdshell   'bcp   "select   *   from   ColinTest..table1   where   datefield>''2005-01-01''   and   datefield<''2005-05-01''   and   strfield=''ccc''   "   queryout   c:\ccc.xsl   -c'  
  exec   master..xp_cmdshell   'bcp   "select   *   from   ColinTest..table1   where   datefield>''2005-01-01''   and   datefield<''2005-05-01''   and   strfield=''ddd''   "   queryout   c:\ddd.xsl   -c'  
  exec   master..xp_cmdshell   'bcp   "select   *   from   ColinTest..table1   where   datefield>''2005-01-01''   and   datefield<''2005-05-01''   and   strfield=''eee''   "   queryout   c:\eee.xsl   -c'  
   
   
   
  Top

6 楼lqxty()回复于 2005-08-04 00:00:39 得分 0

libin_ftsafe(子陌红尘)   :好像不行啊!我在哪里填写我的条件呢?aaa,bbb,ccc,ddd,eee  
  我要的是在这个时间段内,每个符合aaa,bbb,ccc,ddd,eee的条件的数据!  
  就是,在这个时间段内字符串为aaa的有30条数据,存为aaa.xls  
              在这个时间段内字符串为bbb的有1000条数据,存为bbb.xls  
  依此类推!我一定结!Top

7 楼zjcxc(邹建)回复于 2005-08-04 12:40:52 得分 0

我的呢?Top

8 楼chenqianlong(443)回复于 2005-08-04 13:36:20 得分 0

看Top

9 楼phantomMan()回复于 2005-08-04 13:51:58 得分 0

我上面的不行吗?/  
  我自己测试过,完全可以的Top

10 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-08-04 14:06:11 得分 0

以下代码通过测试:  
  ------------------------------------------------------------  
   
  --生成测试数据  
  use   sqldb  
  go  
   
  create   table   table(name   varchar(20),dates   datetime)  
  insert   into   [table]   select   'aaa','2005-01-01'  
  insert   into   [table]   select   'bbb','2005-01-01'  
  insert   into   [table]   select   'ccc','2005-01-01'  
  insert   into   [table]   select   'ddd','2005-01-01'  
  insert   into   [table]   select   'eee','2005-01-01'  
  go  
   
   
  --创建存储过程  
  create   procedure   sp_bcp(@strdate   datetime,@enddate   datetime)  
  as  
  begin  
          declare   @name   varchar(20),@s   varchar(8000)  
          declare   t_cursor   cursor   for    
          select   distinct   name   from   [table]   where   dates   between   @strdate   and   @enddate  
           
          open   t_cursor  
           
          fetch   next   from   t_cursor   into   @name  
           
          while   @@fetch_status   =   0  
          begin  
                  set   @s   =   'bcp   "select   *   from   sqldb..[table]   where   name='''  
                                      +@name+'''   and   dates   between   '''  
                                      +convert(char(10),@strdate,120)+'''   and   '''  
                                      +convert(char(10),@enddate,120)+'''"   queryout   "C:\'  
                                      +@name+'.xls"   -c   -q   -U"sa"   -P"xxx"'     --需替换用户名和密码  
                  print   @s  
                  exec   master..xp_cmdshell   @s,no_output  
                  fetch   next   from   t_cursor   into   @name  
          end  
           
          close   t_cursor  
          deallocate   t_cursor  
  end  
  go  
   
   
  --执行存储过程  
  exec   sp_bcp   '2005-01-01','2005-05-01'  
  goTop

相关问题

  • 存储过程的问题(在线等待,谢谢)
  • 求一存储过程的写法,在线等,先谢了
  • 两个存储过程问题,帮我答了,重分酬谢
  • 存储过程,在线等!!!!!!!!!
  • 在线求存储过程!?
  • 存储过程中怎样再调用存储过程?谢谢
  • 存储过程循环套循环问题,谢谢 (在线等待)
  • 问几个存储过程里头的语法,谢谢,在线给分
  • 求一个存储过程 谢谢高手大侠们  急!!!在线等哈
  • 存储过程问题(在线等候)

关键词

  • 存储过程
  • 字符
  • datefield
  • 日期
  • sqldb
  • eee
  • xls
  • ddd
  • ccc
  • 字符条件

得分解答快速导航

  • 帖主:lqxty
  • zjcxc
  • libin_ftsafe

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo