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

有点难度的问题,请教大家。

楼主delwin(delwin)2004-09-03 11:42:24 在 MS-SQL Server / 应用实例 提问

table1这个表是动态建立的  
  select   @cttable=@cttable+m.months+'   numeric(18,2)   ,'   from    
  (  
  select   distinct   '月'+cast(datepart(month,jydate)   as   varchar(2))+'月'   as   months  
  From   jyinfo  
  where   machnum   in   (select   machnum   from   contract_info   where   contract_num=@contractnum)  
  and   jydate   between   @stdate   and   @eddate  
  )   m  
  得到日期,然后exec()建立的。  
   
   
  table1  
   
  aa         月1月                   月2月                   月3月                    
  ---------------------------------------  
  aaaaa   NULL                 NULL                 NULL  
   
  table2  
  counts             months                                                                                            
  -----------   ---------  
  1                       月1月  
  2                       月2月  
  3                       月3月  
   
  那么我现在想得到这样的结果  
  aa         月1月                   月2月                   月3月                    
  ---------------------------------------  
  aaaaa   1                             2                         3  
   
   
  要注意的是,月份不是固定的。是通过前面选出来的。  
   
   
   
  问题点数:20、回复次数:9Top

1 楼yesterday2000(一笑而过)回复于 2004-09-03 11:55:04 得分 20

------用邹老大的!!  
  declare   @s   varchar(8000)  
  set   @s=''  
  select     @s=@s+',['+cast(months   as   varchar)+']='''+counts+''''  
  from   TB  
  set   @s=stuff(@s,1,1,'')  
  exec('select   '+@s)Top

2 楼delwin(delwin)回复于 2004-09-03 12:03:42 得分 0

多谢,  
  这个我知道,关键是那个UPDATE语句怎么写?Top

3 楼delwin(delwin)回复于 2004-09-03 12:04:16 得分 0

table1  
   
  aa         月1月                   月2月                   月3月                    
  ---------------------------------------  
  aaaaa   NULL                 NULL                 NULL  
   
  table2  
  counts             months                                                                                            
  -----------   ---------  
  1                       月1月  
  2                       月2月  
  3                       月3月  
   
  那么我现在想得到这样的结果  
  aa         月1月                   月2月                   月3月                    
  ---------------------------------------  
  aaaaa   1                             2                         3  
  Top

4 楼yesterday2000(一笑而过)回复于 2004-09-03 12:21:52 得分 0

create   table   tt   (aa   char   (10),月1月   int,月2月   int,月3月   int     )  
  create   table   tz   (counts   int,months   char   (10))  
   
  insert   into   tt(aa)  
  select   'aaaaa'  
   
  insert   into   tz  
  select   1,'月1月'  
  union    
  select   2,'月2月'  
  union    
  select   3,'月3月'  
   
   
   
   
  update   tt   set   月1月=b.月1月,月2月=b.月2月,月3月=b.月1月  
  from   tt   a,  
   
  (  
  select   'aaaaa'   as   aa,a.*,b.*,c.*  
  from    
  (  
  select   counts   as   月1月   from   tz   where   months='月1月')   a,(  
  select   counts   as   月2月   from   tz   where   months='月2月')   b,(  
  select   counts   as   月3月   from   tz   where   months='月3月')   c   )   b  
  where   a.aa=b.aaTop

5 楼delwin(delwin)回复于 2004-09-03 12:31:56 得分 0

多谢   yesterday2000(一笑而过)   :  
  你这个是对的,大家看看还有其它方法吗?Top

6 楼delwin(delwin)回复于 2004-09-03 12:58:35 得分 0

请教大家Top

7 楼yesterday2000(一笑而过)回复于 2004-09-03 12:59:36 得分 0

楼主  
  不好意思  
  我的基础比较低  
  只好用笨方法给你解答!!  
  我想一定有其它简单的方法!!Top

8 楼delwin(delwin)回复于 2004-09-03 13:08:53 得分 0

呵呵,谢谢   yesterday2000(一笑而过),因为我是想用前面那个表是动态建立的,所以用你的那个句子在处理  
  select   'aaaaa'   as   aa,a.*,b.*,c.*  
  from    
  (  
  select   counts   as   月1月   from   tz   where   months='月1月')   a,(  
  select   counts   as   月2月   from   tz   where   months='月2月')   b,(  
  select   counts   as   月3月   from   tz   where   months='月3月')   c   )的时候我觉的我这边可能实现起来比较麻烦,所以就想看看还有没有其他方法,谢谢你。Top

9 楼delwin(delwin)回复于 2004-09-03 13:10:56 得分 0

都怪我刚才没有说明,向yesterday2000(一笑而过),致歉。呵呵Top

相关问题

  • 有点难度
  • 有点难度
  • 有点难度
  • 有点难度哦!
  • 有点难度的...
  • 可能有点难度!!
  • 有点难度的问题
  • 有点难度的问题!
  • 有点难度的问题,
  • 触发器,有点难度

关键词

  • null
  • 月
  • aaaaa
  • months
  • aa
  • yesterday2000
  • tz where
  • tt
  • counts
  • 方法

得分解答快速导航

  • 帖主:delwin
  • yesterday2000

相关链接

  • SQL Server类图书

广告也精彩

反馈

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