jsonsql-servert-sqlsql-server-2016compatibility-level

OPENJSON in compatibility level 100 SQL SERVER 2016


I need to use the functionality of OPENJSON() in an old database with compatibility level 100. The server runs SQL SERVER 2016. So i came up with this idea: Create another DB "GeneralUTILS" (lvl 130) in the same server and call this function from lvl 100 DB:

CREATE FUNCTION [dbo].[OPENJSON_](@json NVARCHAR(MAX))
RETURNS   @Results TABLE ([Key] nVARCHAR (4000)  , [Value] NVARCHAR(MAX), [Type] INT)
AS
BEGIN
    INSERT INTO @Results 
    SELECT * from OPENJSON(@json) 

    RETURN
END

But i don't have the WITH clause to modify the output table in the lvl 100 database.


Solution

  • Most important might be the question why you need this at all...

    I hope I got correctly, what you need:

    (Hint: This needs at least SQL-Server 2016)

    --create two mock-up-databases

    CREATE DATABASE dbOld;
    GO
    ALTER DATABASE dbOld SET COMPATIBILITY_LEVEL = 100; --v2008
    GO
    
    CREATE DATABASE dbForJsonIssues;
    GO
    ALTER DATABASE dbForJsonIssues SET COMPATIBILITY_LEVEL = 130; --v2016
    GO
    

    --Now we will create a stored procedure in the "higher" database

    USE dbForJsonIssues;
    GO
    --Attention: replacing FROM is a very hacky way... Read the hints at the end...
    --You might use parameters for the JSON-string and the JSON-path, but then you must use sp_executesql
    CREATE PROCEDURE EXEC_Json_Command @Statement NVARCHAR(MAX), @TargetTable NVARCHAR(MAX)
    AS
    BEGIN
        DECLARE @statementWithTarget NVARCHAR(MAX)=REPLACE(@Statement,'FROM',CONCAT(' INTO ',@TargetTable,' FROM'));
        PRINT @statementWithTarget; --you can out-comment this line...
        EXEC(@statementWithTarget);
    END
    GO
    

    --Now we go into the "lower" database

    USE dbOld;
    GO
    
    --A synonym is not necessary, but allows for easier code
    CREATE SYNONYM dbo.ExecJson FOR dbForJsonIssues.dbo.EXEC_Json_Command;
    GO
    

    --This is how to use it

    DECLARE @json NVARCHAR(MAX)=N'[{"someObject":[{"attr1":"11", "attr2":"12"},{"attr1":"21", "attr2":"22"}]}]';
    
    DECLARE @Statement NVARCHAR(MAX)=CONCAT(N'SELECT * FROM OPENJSON(N''',@json,N''',''$[0].someObject'') WITH(attr1 INT,attr2 INT)');
    
    --the target table will be created "on the fly"
    --You can use ##SomeTarget too, but be careful with concurrencies in both approaches...
    EXEC ExecJson @Statement=@Statement,@TargetTable='dbOld.dbo.SomeTarget';
    SELECT * FROM SomeTarget;
    
    --We can drop this table after dealing with the result
    DROP TABLE SomeTarget;
    GO
    

    --Clean-up (carefull with real-data!)

    USE master;
    GO
    DROP DATABASE dbOld;
    DROP DATABASE dbForJsonIssues;
    

    The most important concepts:

    We cannot use the JSON-statements directly within the database, but we can create a statement on string base, pass it to the stored procedure and use EXEC() for its execution.

    Using SELECT * INTO SomeDb.SomeSchema.SomeTargetTable FROM ... will create a table with the fitting structure. Make sure to use a table not existing in your database.

    It is not really needed to pass the target table as parameter, you might place this in the statement yourself. Replacing the FROM in the stored procedure is a very shrewed way and could lead into troubles if from is found in another place.

    You might use similar procedures for various needs...