表:colId(int)    colMoney(money)    colDate(datetime)
1             100                2009/10/23 23:33
2             -20                2009/10/24 1:22
3             150                2009/10/28 9:33
4             -90                2009/10/30 20:00
5             -200               2010/1/1 12:22
6             300                2010/2/3 13:00需要写一个存储过程:CREATE PROCEDURE getMonthTotal
(
    @AbsoluteMonth INT
)
AS传入的 @AbsoluteMonth 是外部由 年*月 得来,比如2009年的10月,外部会先进行 2009*10 的计算,然后传入 20090 给 @AbsoluteMonth。现在要求通过这个 @AbsoluteMonth,查询表内所有符合的条件(例如20090,就是要查询表内所有 2009年10月的数据)。

解决方案 »

  1.   

    --> 测试数据:@t
    declare @t table([colId(int)] int,[colMoney(money)] int,[colDate(datetime)] datetime)
    insert @t
    select 1,100,'2009/10/23 23:33' union all
    select 2,-20,'2009/10/24 1:22' union all
    select 3,150,'2009/10/28 9:33' union all
    select 4,-90,'2009/10/30 20:00' union all
    select 5,-200,'2010/1/1 12:22' union all
    select 6,300,'2010/2/3 13:00'
     
     
    select * from @t where year([colDate(datetime)])*MONTH([colDate(datetime)]) = @int ---------------、
    1 100 2009-10-23 23:33:00.000
    2 -20 2009-10-24 01:22:00.000
    3 150 2009-10-28 09:33:00.000
    4 -90 2009-10-30 20:00:00.000
      

  2.   

    是年月查詢數據?
    go
    CREATE PROCEDURE getMonthTotal
    (
      @AbsoluteMonth varchar(6)
    )
    AS
    select * from table1 where convert(varchar(6),colDate,112)=@AbsoluteMonth 
    goexec getMonthTotal '201009'
      

  3.   


    select * from tableName where year(colDate)*Month(colDate)=@AbsoluteMonth