我需要将批量数据拆分为2个DB作为当前和历史

时间:2014-07-08 15:20:11

标签: sql

**

**if (month(getdate())=01) and (day(getdate())=01)
declare @i as datetime
declare @j as datetime
set @i = getdate()
set @j=select year(@i)-3
---set @j=@i
---set @i
select '@j'+''+'dec'+''+'31'
select distinct * into <destination> from <source> where <datecolumn><= '@j'+'-'+'dec'+'-'+'31'**

**

这就是我现在所做的,但问题是我需要每年从当前的数据中追加数据

1 个答案:

答案 0 :(得分:0)

只需使用INSERT INTO

INSERT INTO <destination> 
SELECT DISTINCT * 
FROM <source> 
WHERE <datecolumn> <= @j + '-dec-31'

请注意,您的原始语法是将@j视为 literal 字符串,而不是将其替换为数字年份值。