日期:2014-05-17 浏览次数:20619 次
DECLARE @t TABLE(品名 VARCHAR(10),sd DATETIME ,ed DATETIME);
INSERT INTO @t SELECT 'a','2012-01-01','2012-05-05'
UNION ALL SELECT 'a','2012-02-06','2012-03-06'
UNION ALL SELECT 'a','2012-02-15','2012-05-05'
UNION ALL SELECT 'b','2012-02-06','';
--比入插入一条 'a','2012-02-20',''
INSERT INTO @t SELECT 'a','2012-02-20','';
SELECT * FROM @t;
;WITH cte AS (
SELECT rn=ROW_NUMBER() OVER(PARTITION BY 品名 ORDER BY sd ),* FROM @T
)
UPDATE a SET a.ed=b.sd-1 FROM cte a LEFT JOIN cte b ON a.品名=b.品名 AND a.rn=b.rn-1
WHERE b.品名='a' AND b.sd='2012-02-20' AND b.ed='' --这里是条件
SELECT * FROM @t