想写个存储过程,每天晚上0点运行一次,具体内容如下表UserStatInfo,结构如下
UserId(int) StartDate(smalldatetime) Money(decimal)目的功能:
1、检查表中所有注册日(StartDate)起31、35、40天(以当前日期为准)内金额(Money)未满100的UserId,发送邮件提醒(用SQL发MAIL)
2、检查表中所有注册日(StartDate)起42天内金额(Money)未满100的UserId,将金额(Money)重置为0,并将注册日期(StartDate)重置为当前日期

解决方案 »

  1.   

    1、检查表中所有注册日(StartDate)起31、35、40天(以当前日期为准)内金额(Money)未满100的UserId,发送邮件提醒(用SQL发MAIL) 
    --- 40天以内就包含了前面31天和35天的了,所以,是否是只需要查询在40天以内就行了,
    select * from UserStatInfo where datediff(day,[StartDate],getdate())<=40 and [Money]<100 
    SQL发Mail的话,要开启SQL的mail功能和配置Database Mail,
    调用,xp_sendmail
    2、检查表中所有注册日(StartDate)起42天内金额(Money)未满100的UserId,将金额(Money)重置为0,并将注册日期(StartDate)重置为当前日期 
    update UserStatInfo set [money] = 0,[StartDate] =getdate() where datediff(day,[StartDate],getdate())<=40 and [Money]<100 
      

  2.   


    declare @test table (UserId int , StartDate smalldatetime , M_oney decimal  )
    insert into @test 
    select 1,getdate()-31,10 union all
    select 2, getdate()-31,200 union all
    select 3,getdate()-35,20 union all
    select 4,getdate()-35,300 union all
    select 5,getdate()-40,10 union all
    select 6,getdate()-40,400 union all
    select 7,getdate()-42,10 union all
    select 8,getdate()-43,300 select * from @test
      

  3.   

    注意日期,是从31-40?还是就是31、35、40?对于大于42的就好处理了。datediff
      

  4.   

    1、检查表中所有注册日(StartDate)起31、35、40天(以当前日期为准)内金额(Money)未满100的UserId,发送邮件提醒(用SQL发MAIL)
    思路:我觉得6楼的对lz要求的理解有误。6楼的条件将会把刚刚注册的人也列出来。既然是提醒,自然不会天天给他们提醒,而是在第31天、第35天、第40天三次提醒,提醒他们如果再不消费,积分将被置零。Select *, datediff(day,[StartDate],getdate())
    FROM UserStatInfo
    WHERE (datediff(day,[StartDate],getdate()) = 31 or datediff(day,[StartDate],getdate()) = 35 or datediff(day,[StartDate],getdate()) = 40) AND Money < 100
    2、检查表中所有注册日(StartDate)起42天内金额(Money)未满100的UserId,将金额(Money)重置为0,并将注册日期(StartDate)重置为当前日期
    思路:6楼的方法应该也是错了,应该是在用户注册第42天,消费额累计仍然未满100,才将Money置零。因此应该修改一下条件,<=40改为 =42
    update UserStatInfo set [money] = 0,[StartDate] =getdate() where datediff(day,[StartDate],getdate())=42 and [Money]<100 
      

  5.   

    谢谢各位解答,麻烦看看以下的这个网址,和这个问题相似。
    http://topic.csdn.net/u/20080626/10/c4d81914-98d6-4971-b4b6-b378662178ee.html