每月将有限(1000 行)块的 SQL Server 数据移动到不同的表

发布于 2024-12-18 07:04:29 字数 751 浏览 2 评论 0原文

我正在寻找一种解决方案,如何根据部分日期时间值(作为每月存档)将行从 1000 块的大表移动到不同的表。我正在使用 MS SQL Server 2008。Remus

Rusanu 在 stackoverflow 上提供了以下解决方案 以有限(1000 行)块的形式移动 SQL Server 数据,用于以块的形式移动行。工作起来就像一个魅力:-)

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData)
    DELETE messages
        OUTPUT DELETED.id, messageDatetime, message
        INTO messageArchive;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

我现在需要的是能够根据 messageDate 的月份部分将行移动到不同的表。

  • 九月的消息应插入名为 messageArchive_09 的表中
  • 十月的消息应插入名为 messageArchive_10 的表中
  • ...

有什么想法吗?

I'm looking for a solution how I can move rows from a large table in chunks of 1000 to different tables based on parts of a datetime value (as a monthly archive). I'm using MS SQL Server 2008.

Remus Rusanu provided the following solution here on stackoverflow Move SQL Server data in limited (1000 row) chunks for moving rows in chunks. Works like a charm :-)

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData)
    DELETE messages
        OUTPUT DELETED.id, messageDatetime, message
        INTO messageArchive;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

What I need now ist the ability to move the rows to different tables depending on the month part of the messageDate.

  • messages from september should be inserted into a table named messageArchive_09
  • messages from october should be inserted into a table named messageArchive_10
  • ...

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2024-12-25 07:04:29

只需使用 month() 和一个不同的目标表。我知道,复制和粘贴代码感觉很糟糕,替代方法是做一些动态

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = 1)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO messageArchive_01;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = 2)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO messageArchive_02;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

如果你想要动态,可以使用一些代码

declare @sql nvarchar(max)
declare @Template nvarchar(max) ='
WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = <MONTH>)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO <TABLENAME>;

    IF (@@ROWCOUNT = 0)
        BREAK;
END'

declare @I int = 1

while @I <= 12
begin
  set @sql = replace(@Template, '<TABLENAME>', 'messageArchive_'+right(100+@I, 2))
  set @sql = replace(@sql, '<MONTH>', @I)

  exec (@sql)

  set @I += 1
end

Just repeat the code you have 12 times with a where clause using month() and a different target table. I know, copy and paste code feels bad, the alternative is to do something dynamic.

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = 1)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO messageArchive_01;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = 2)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO messageArchive_02;

    IF (@@ROWCOUNT = 0)
        BREAK;
END

Some code to use if you want to go dynamics

declare @sql nvarchar(max)
declare @Template nvarchar(max) ='
WHILE 1=1
BEGIN
    WITH messages AS (
    SELECT TOP 1000 id, messageDatetime, message 
        FROM DemoData
        WHERE month(messageDatetime) = <MONTH>)
    DELETE messages
        OUTPUT DELETED.id, DELETED.messageDatetime, DELETED.message
        INTO <TABLENAME>;

    IF (@@ROWCOUNT = 0)
        BREAK;
END'

declare @I int = 1

while @I <= 12
begin
  set @sql = replace(@Template, '<TABLENAME>', 'messageArchive_'+right(100+@I, 2))
  set @sql = replace(@sql, '<MONTH>', @I)

  exec (@sql)

  set @I += 1
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文