日期:2014-05-18 浏览次数:20542 次
CREATE FUNCTION generateTimeV2 ( @begin_date DATETIME , @end_date DATETIME ) RETURNS @t TABLE ( date DATETIME ) AS BEGIN INSERT INTO @t SELECT DATEADD(dd, number, @begin_date) AS date FROM master..spt_values WHERE type = 'p' AND DATEADD(dd, number, @begin_date) <= @end_date RETURN END declare @T table (name varchar(14),date datetime) insert into @T select '张三','2011-10-26' union all select '张三','2011-10-29' union all select '张三','2011-10-31' SELECT b.* FROM @T a RIGHT JOIN ( SELECT '张三' AS name , * FROM dbo.generateTimeV2(( SELECT MIN(date) FROM @T WHERE name = '张三' ), ( SELECT MAX(date) FROM @T WHERE name = '张三' )) ) b ON a.NAME = b.NAME AND a.date = b.date WHERE a.NAME IS NULL /* name date ---- ----------------------- 张三 2011-10-27 00:00:00.000 张三 2011-10-28 00:00:00.000 张三 2011-10-30 00:00:00.000 */