Skip to content

Add support for JSON data type - #118

Open
drmcclelland wants to merge 1 commit into
dnlnln:masterfrom
drmcclelland:main
Open

Add support for JSON data type#118
drmcclelland wants to merge 1 commit into
dnlnln:masterfrom
drmcclelland:main

Conversation

@drmcclelland

@drmcclelland drmcclelland commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

With the new JSON Data Type available in SQL Server 2025 and in Azure SQL, a column can contain JSON objects:

{"brand": "Dell", "specs": {"ram": "16GB", "storage": "512GB"}, "tags": ["electronics", "work"]}

and JSON arrays:

["Dell","Lenovo","Apple"]

Sample table data:

CREATE TABLE [dbo].[ProductCatalog](
	[ProductID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[Attributes] [json] NOT NULL)

INSERT INTO [dbo].[ProductCatalog] (Attributes) VALUES ('{"brand": "Dell", "specs": {"ram": "16GB", "storage": "512GB"}, "tags": ["electronics", "work"]}')
INSERT INTO [dbo].[ProductCatalog] (Attributes) VALUES ('["Dell","Lenovo","Apple"]')

This PR aims to resolve the following errors when executing the sp_generate_merge stored procedure:

EXEC [sp_generate_merge] 
     @Schema='dbo',
     @Table_Name='ProductCatalog'
  1. Currently we get errors like this when a JSON column contains an object:
Msg 13639, Level 16, State 1, Line 1125
Target string size is too small to represent the JSON instance.
  1. Even if we remove rows containing JSON objects from the table, we still get improperly generated SQL when a JSON column contains an array:
image

With this fix, we get the following generated SQL:

SET IDENTITY_INSERT [dbo].[ProductCatalog] ON

MERGE INTO [dbo].[ProductCatalog] AS [Target]
USING (VALUES
  (2,CAST(N'["Dell","Lenovo","Apple"]' AS JSON))
 ,(3,CAST(N'{"brand":"Dell","specs":{"ram":"16GB","storage":"512GB"},"tags":["electronics","work"]}' AS JSON))
) AS [Source] ([ProductID],[Attributes])
ON ([Target].[ProductID] = [Source].[ProductID])
WHEN MATCHED AND EXISTS (SELECT CAST([Source].[Attributes] AS NVARCHAR(MAX))
                 EXCEPT  SELECT CAST([Target].[Attributes] AS NVARCHAR(MAX))) THEN
 UPDATE SET
  [Target].[Attributes] = [Source].[Attributes]
WHEN NOT MATCHED BY TARGET THEN
 INSERT([ProductID],[Attributes])
 VALUES([Source].[ProductID],[Source].[Attributes])
WHEN NOT MATCHED BY SOURCE THEN 
 DELETE;


SET IDENTITY_INSERT [dbo].[ProductCatalog] OFF

@drmcclelland
drmcclelland marked this pull request as ready for review July 28, 2026 22:34
@drmcclelland drmcclelland changed the title Add support for json data type Add support for JSON data type Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant