0

我有一个带有内存优化表的数据库。我想将此表存档在另一个数据库中。我想写一个存储过程来做到这一点。

我在下面的示例12中成功实现,但在这些示例中,第一个数据库不在内存中,第二个数据库在内存中。就我而言,第一个数据库在内存中,第二个数据库可以在内存中,也可以不在内存中。

这是我的代码:

1-我的桌子:

USE [TestReport]
GO

/****** Object:  Table [dbo].[Report]    Script Date: 1/22/2018 4:40:04 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Report]
(
    [ReportID] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [Year] [int] NOT NULL,
    [DayOfYear] [int] NOT NULL,
    [ProductType] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [ApplicationID] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [TotalSize] [bigint] NOT NULL DEFAULT ((0)),
    [TotalCount] [bigint] NOT NULL DEFAULT ((0)),
    [LastReportTimeSpan] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NULL,

INDEX [idx] NONCLUSTERED HASH 
(
    [ReportID],
    [DayOfYear]
)WITH ( BUCKET_COUNT = 131072),
CONSTRAINT [pk] PRIMARY KEY NONCLUSTERED HASH 
(
    [ReportID],
    [Year],
    [DayOfYear],
    [ProductType],
    [ApplicationID]
)WITH ( BUCKET_COUNT = 131072)
)WITH ( MEMORY_OPTIMIZED = ON , DURABILITY = SCHEMA_AND_DATA )

GO

2-简单的存储过程

CREATE PROCEDURE [dbo].[ArchiveReport]
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS BEGIN ATOMIC WITH
(
 TRANSACTION ISOLATION LEVEL =  SNAPSHOT, LANGUAGE =  N'us_english'
)
     BEGIN
         DECLARE @currentdate DATETIME2;
         SET @currentdate = GETDATE();
        declare @maintainDay INT = 5

    INSERT  TestReportArchive.[dbo].Report
         SELECT [ReportID],
                [Year],
                [DayOfYear],
                [ProductType],
                [ApplicationID],
                [TotalSize],
                [TotalCount],
            [LastReportTimeSpan]
          FROM [dbo].[Report] 
         WHERE DATEADD(day, [DayOfYear] + @maintainDay, DATEADD(YEAR, [Year] - 1900, 0)) > @currentdate;

         DELETE FROM [dbo].[Report]
         WHERE DATEADD(day, [DayOfYear] + @maintainDay, DATEADD(YEAR, [Year] - 1900, 0)) > @currentdate;

     END;
END

3-简单的存储过程错误

Msg 4512, Level 16, State 3, Procedure ArchiveReport, Line 12
Cannot schema bind procedure 'dbo.ArchiveReport' because name 'TestReportArchive.dbo.Report' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.

TestReportArchive is my destination database

4-使用12。表变量的定义

USE [TestReport]
GO

/****** Object:  UserDefinedTableType [dbo].[MemoryType]    Script Date: 1/22/2018 4:35:14 PM ******/
CREATE TYPE [dbo].[MemoryType] AS TABLE(
    [ReportID] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [Year] [int] NOT NULL,
    [DayOfYear] [int] NOT NULL,
    [ProductType] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [ApplicationID] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NOT NULL,
    [TotalSize] [bigint] NOT NULL,
    [TotalCount] [bigint] NOT NULL,
    [LastReportTimeSpan] [nvarchar](50) COLLATE Latin1_General_100_BIN2 NULL,
    INDEX [idx] NONCLUSTERED HASH 
(
    [ReportID],
    [DayOfYear]
)WITH ( BUCKET_COUNT = 131072)
)
WITH ( MEMORY_OPTIMIZED = ON )
GO

5-带表变量的存储过程

CREATE PROCEDURE [dbo].[ArchiveReport]
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS BEGIN ATOMIC WITH
(
 TRANSACTION ISOLATION LEVEL =  SNAPSHOT, LANGUAGE =  N'us_english'
)
     BEGIN
         DECLARE @currentdate DATETIME2;
         SET @currentdate = GETDATE();
        declare @maintainDay INT = 5

         DECLARE @InMem [dbo].[MemoryType];

        INSERT @InMem
        SELECT [ReportID],
                [Year],
                [DayOfYear],
                [ProductType],
                [ApplicationID],
                [TotalSize],
                [TotalCount],
            [LastReportTimeSpan]
         FROM [dbo].[Report] 
         WHERE DATEADD(day, [DayOfYear] + @maintainDay, DATEADD(YEAR, [Year] - 1900, 0)) > @currentdate;
         INSERT  TestReportArchive.[dbo].[Report]
         SELECT [ReportID],
                [Year],
                [DayOfYear],
                [ProductType],
                [ApplicationID],
                [TotalSize],
                [TotalCount],
            [LastReportTimeSpan]
         FROM @InMem

         DELETE FROM [dbo].[Report]
         WHERE DATEADD(day, [DayOfYear] + @maintainDay, DATEADD(YEAR, [Year] - 1900, 0)) > @currentdate;

     END;
END

6- 来自 5 个存储过程的错误

Msg 4512, Level 16, State 3, Procedure ArchiveReport, Line 25
Cannot schema bind procedure 'dbo.ArchiveReport' because name 'TestReportArchive.dbo.Report' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.

TestReportArchive is my destination database
4

2 回答 2

2

不支持涉及内存优化表的跨数据库查询。

内存中 OLTP 不支持的 SQL Server 功能

如果查询使用内存优化表或本机编译的存储过程,则查询无法访问其他数据库。此限制适用于事务以及查询。

于 2018-05-02T12:31:46.080 回答
1

最终,我在 testReport(第一个数据库)上创建了一个非内存优化表 (ReportTemp),并更改了存储过程以将数据从 Report Table 插入到第一个数据库中的 ReportTemp Table。然后我编写另一个 SP 将数据移动到存档数据库。

于 2018-05-07T04:13:57.830 回答