sql-server-2014memory-optimized-tables

cross database query on sql server 2014


I have a database with a memory optimized table. I want to archive this table in another database. I want to write an stored procedure to do that.

I am implemented below sample from 1 and 2 successfully, but in these sample, the first database is not in memory and the second database is in memory. In my case, the first database is in memory and the second one can be in memory or not.

Here is my code:

1- my table :

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- simple Stored procedure

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- simple stored procedure error

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- using 1 and 2. definition of table variable

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- stored procedure with table variable

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- Error from 5 stored procedure

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

Solution

  • Ultimately I created a non-memory-optimized table (ReportTemp) on the testReport (first Database) and change the stored procedure to insert data from Report Table to ReportTemp Table in the first database. Then I write another SP to move Data to archive Database.