将函数的结果插入表中

时间:2013-05-02 05:24:16

标签: sql date user-defined-functions

我从下表值函数中获得了24个月的结果。我需要在另一个表中插入这些月份,结果应该是这样的:

Week     Period
1       2010-02-01
2       2010-02-01
3       2010-02-01
4       2010-02-01
5       2010-03-01
6       2010-03-01
7       2010-03-01
8       2010-03-01
9       2010-03-01
10      2010-04-01
11      2010-04-01
12      2010-04-01
13      2010-04-01
14      2010-05-01
15      2010-05-01
16      2010-05-01
17      2010-05-01
18      2010-06-01
19      2010-06-01
20      2010-06-01
21      2010-06-01
22      2010-06-01
23      2010-07-01
24      2010-07-01
25      2010-07-01
26      2010-07-01
27      2010-08-01
28      2010-08-01
29      2010-08-01
30      2010-08-01
31      2010-09-01
32      2010-09-01
33      2010-09-01
34      2010-09-01
35      2010-09-01
36      2010-10-01
37      2010-10-01
38      2010-10-01
39      2010-10-01
40      2010-11-01
41      2010-11-01
42      2010-11-01
43      2010-11-01
44      2010-12-01
45      2010-12-01
46      2010-12-01
47      2010-12-01
48      2010-12-01
49      2011-01-01
50      2011-01-01
51      2011-01-01
52      2011-01-01
53      2011-02-01
54      2011-02-01
55      2011-02-01
56      2011-02-01
57      2011-03-01
58      2011-03-01
59      2011-03-01
60      2011-03-01
61      2011-03-01
62      2011-04-01
63      2011-04-01
64      2011-04-01
65      2011-04-01
66      2011-05-01
67      2011-05-01
68      2011-05-01
69      2011-05-01
70      2011-06-01
71      2011-06-01
72      2011-06-01
73      2011-06-01
74      2011-06-01
75      2011-07-01
76      2011-07-01
77      2011-07-01
78      2011-07-01
79      2011-08-01
80      2011-08-01
81      2011-08-01
82      2011-08-01
83      2011-09-01
84      2011-09-01
85      2011-09-01
86      2011-09-01
87      2011-09-01
88      2011-10-01
89      2011-10-01
90      2011-10-01
91      2011-10-01
92      2011-11-01
93      2011-11-01
94      2011-11-01
95      2011-11-01
96      2011-12-01
97      2011-12-01
98      2011-12-01
99      2011-12-01
100     2011-12-01
101     2012-01-01
102     2012-01-01
103     2012-01-01
104     2012-01-01

我的功能如下:

ALTER FUNCTION [dbo].[sun_care1](@start_period date, @end_period date)
RETURNS @date TABLE (
    Period date NOT NULL
) 
AS
BEGIN


--& DATEADD (MONTH,1,@start_period)
    WHILE (@end_period <= @start_period) BEGIN

     SET @end_period= DATEADD(MONTH,1,@end_period)

       --set @start_period =CAST(@start_period as varchar(max))
       INSERT INTO @date VALUES  (@end_period)
    END;

    RETURN;
END;

1 个答案:

答案 0 :(得分:0)

在SQLServer2005 +中使用CTE和master..spt_values系统表。您也可以使用序列表而不是master..spt_values系统表。

修改功能

ALTER FUNCTION [dbo].[sun_care1](@start_period date, @end_period date)
RETURNS @date TABLE (Period date NOT NULL) 
AS
BEGIN
  --& DATEADD (MONTH,1,@start_period)
  WHILE (@start_period <= @end_period)
  BEGIN
    SET @start_period= DATEADD(MONTH, 1, @start_period)
    --set @start_period =CAST(@start_period as varchar(max))
    INSERT INTO @date 
    VALUES(@start_period)
  END;
  RETURN;
END;

使用CTE和master..spt_values系统表的INSERT语句

DECLARE @start_period date = '20100101',
        @end_period date = '20111201'  
;WITH cte AS
 (
  SELECT -1 AS rn
  UNION ALL
  SELECT rn + 3
  FROM cte
  WHERE rn < DATEDIFF(MONTH, @start_period, @end_period) * 3
  ), cte2 AS
 (
  SELECT *, ROW_NUMBER() OVER (ORDER BY Period) AS rn 
  FROM [dbo].[sun_care1](@start_period, @end_period)
  )
  INSERT AnotherTableName(columnName)
  SELECT Period     
  FROM cte2 s LEFT JOIN cte c ON s.rn = c.rn
              CROSS APPLY (
                           SELECT number
                           FROM master..spt_values v
                           WHERE v.type = 'P'
                             AND v.number < CASE WHEN c.rn IS NULL 
                                                 THEN 4 ELSE 5 END
                           ) o