在我的一个较大的表中,旧记录被移动到以季度和年份为后缀的单独表中。例如,SILO_q1_2014
对于历史查询,我通常使用包含当前表和所有历史表的视图。这将包括上一季度,例如q2_2014,但是一旦我们进入2014Q4并且创建了新的q3_2014表,它将会过时。
那么最好的方法是保持这个视图与最新季度的最新信息是什么?表吗
ALTER VIEW [dbo].[v_SILO]
AS
SELECT *
FROM dbo.SILO
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q2_2014
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q1_2014
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q4_2012
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q3_2012
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q2_2012
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q1_2012
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q4_2011
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q3_2011
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q2_2011
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q1_2011
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q4_2010
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q3_2010
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q2_2010
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q1_2010
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q4_2009
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q3_2009
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q2_2009
WHERE active_flag = 1
UNION ALL
SELECT *
FROM dbo.SILO_q1_2009
WHERE active_flag = 1
GO
注意:这是在 SQL 2000 db
中答案 0 :(得分:0)
假设您有一个标准的命名约定,您可以创建一个存储过程,该过程将丢弃并重新创建视图以包含当前季度。然后,将该存储过程作为预定作业放在每月运行一次的SQL Server中。