| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1785 人关注过本帖
标题:SQL 语句 难题!!!求解!
只看楼主 加入收藏
amwihfku
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-2-25
结帖率:0
收藏
 问题点数:0 回复次数:6 
SQL 语句 难题!!!求解!
现有一个表内容如下:

AA        BB1           BB2         
已收     2008-01-01     2008-01-31
已收     2008-02-01     2008-02-29
已收     2008-04-01     2008-04-30
已收     2008-07-01     2008-07-31
假如现在的时间是 2008-08-10,我想让SQL判断出表中没有数据的月份,并插入一行数据,要求结果如下:
AA        BB1           BB2         
已收     2008-01-01     2008-01-31
已收     2008-02-01     2008-02-29
未收     2008-03-01     2008-03-31
已收     2008-04-01     2008-04-30
未收     2008-05-01     2008-05-31
未收     2008-06-01     2008-06-30

已收     2008-07-01     2008-07-31

如何可以实现?
搜索更多相关主题的帖子: SQL 语句 难题 求解 
2008-10-09 08:42
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
create function fn_insertnorecord(@nowtime as datetime)
returns varchar(50)
as
begin
    declare @nowmonth as int
    declare @norecord as varchar(50)
    declare @i    as    int
    set @i=1;
    set @norecord=''
    select @nowmonth=datepart(month,@nowtime);--取得当前的月份
    declare tables_cursor cursor
    for        
        select datepart(month,BB1) from SQl_1 where BB1<=@nowtime order by BB1
    open tables_cursor
    declare @month int
    fetch tables_cursor into @month    
    while (@@fetch_status=0)
    begin        
        if @month<>@i
            begin
                set @norecord=@norecord+cast(@i as varchar)+','                
            end
        else
            begin
                fetch next from tables_cursor into @month
            end
        set @i=@i+1;    
    end
    close tables_cursor
    deallocate  tables_cursor    
    return @norecord
end
go

有实力才会有魅力 实力来自坚持不懈的努力
2008-10-09 10:37
小仙
Rank: 7Rank: 7Rank: 7
来 自:光之谷
等 级:贵宾
威 望:39
帖 子:1812
专家分:1
注 册:2008-8-8
收藏
得分:0 
呃。。借鉴了LS的,写了个:
declare @ibb1 datetime   --要插入的bb1字段变量
declare @ibb2 datetime   --要插入的bb2字段变量
declare @Month int       --月份变量

set @Month = 1

while @month <= 10
begin
  if not exists (select * from Delmonth where bb1 = cast('2008-'+ltrim(str(@month))+'-1' as datetime ))
  begin
    set @ibb1 = cast('2008-'+ltrim(str(@month))+'-1' as datetime )
    set @ibb2 = dateadd(dd,-1,dateadd(mm,1,@ibb1))
    insert into Delmonth(AA,BB1,BB2) values('未收',@ibb1,@ibb2)
  end
  set @Month = @month + 1
end
当然这个程序只适合这个条件。


仁者乐膳,智者乐睡。我都爱~
2008-10-11 18:28
wang2044
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2008-10-21
收藏
得分:0 
好羡慕你们哦!
2008-10-24 01:10
nbxkele
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2008-9-10
收藏
得分:0 
2008-10-24 08:17
provoke
Rank: 2
来 自:广州
等 级:新手上路
威 望:5
帖 子:175
专家分:0
注 册:2007-6-14
收藏
得分:0 
还不如自己订制一个任务计划,定时执行

爱我至爱,至死不渝!
2008-10-25 14:42
魏军元
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2008-10-30
收藏
得分:0 
好强啊!我以后有问题,也请大家多帮忙啊!
2008-10-30 19:29
快速回复:SQL 语句 难题!!!求解!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023597 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved