Search code examples
sqlsql-serversql-server-2014in-memory-databaseoltp

How to compare varchar parameters in a natively compiled stored procedure?


I am migrating some tables and stored procedures to in-memory optimized tables and natively compiled stored procedures. I'm getting stuck on string comparison.

Comparison, sorting, and manipulation of character strings that do not use a *_BIN2 collation is not supported with natively compiled stored procedures.

CREATE TABLE [User].[FingerPrints]
(
    Id int NOT NULL PRIMARY KEY NONCLUSTERED HASH WITH (BUCKET_COUNT = 10000),
    FingerPrint VARCHAR(4000) NOT NULL,
    SecretKey VARCHAR(512) NOT NULL,
    BranchId int NOT NULL,
    SteadySign bit NOT NULL,
    CreatedOn datetime NOT NULL,
    ModifiedOn datetime NOT NULL
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA)
GO

CREATE PROCEDURE [User].[GetFingerPrint]
    @Id int,
    @SecretKey VARCHAR(512)
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS BEGIN ATOMIC WITH
(
 TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english'
)

SELECT [Id]
      ,[FingerPrint]
      ,[SecretKey]
      ,[BranchId]
      ,[SteadySign]
      ,[CreatedOn]
      ,[ModifiedOn]
  FROM [User].[FingerPrints]
  WHERE [Id] = @Id AND [SecretKey] = @SecretKey

END
GO

Solution

  • Changing the collation of the @SecretKey parameter appears to work for me, in a copy of AdventureWorks2014:

    CREATE PROCEDURE [User].[GetFingerPrint]
        @Id int,
        @SecretKey VARCHAR(512) 
        WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
    AS BEGIN ATOMIC WITH
    (
     TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'us_english'
    )
    
    SELECT [Id]
          ,[FingerPrint]
          ,[SecretKey]
          ,[BranchId]
          ,[SteadySign]
          ,[CreatedOn]
          ,[ModifiedOn]
      FROM [User].[FingerPrints]
      WHERE [Id] = @Id AND [SecretKey] = @SecretKey COLLATE LATIN1_GENERAL_BIN2
    
    END
    GO