dnlnln / generate-sql-merge

Generate SQL MERGE statements with Table data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SQL Server v2019 issues

MikeMcWilliams opened this issue · comments

This proc is awesome as I've used it in the past; however, seems to have issues with SQL Server v2019. (I am using an unaltered version of sp_generate_merge). Any assistance is greatly appreciated.

Output from sp_generate_merge

I have 16 rows in the table (see Table Definition section below) and the output of the script does not have data from the table in the USING (VALUES... section. The script does however perform an output in the grid that does represent the data.

--MERGE generated by 'sp_generate_merge' stored procedure, Version 0.9
--Originally by Vyas (http://vyaskn.tripod.com): sp_generate_inserts (build 22)
--Adapted for SQL Server 2008 by Daniel Nolan (http://danere.com)
 
SET NOCOUNT ON
 
SET IDENTITY_INSERT [Status] ON
 
MERGE INTO [Status] AS Target
USING (VALUES
) AS Source ([Id],[Name])
ON (Target.[Id] = Source.[Id])
WHEN MATCHED AND (Target.[Name] <> Source.[Name]) THEN
 UPDATE SET
 [Name] = Source.[Name]
WHEN NOT MATCHED BY TARGET THEN
 INSERT([Id],[Name])
 VALUES(Source.[Id],Source.[Name])
WHEN NOT MATCHED BY SOURCE THEN 
 DELETE;
 
GO
DECLARE @mergeError int
 , @mergeCount int
SELECT @mergeError = @@ERROR, @mergeCount = @@ROWCOUNT
IF @mergeError != 0
 BEGIN
 PRINT 'ERROR OCCURRED IN MERGE FOR [Status]. Rows affected: ' + CAST(@mergeCount AS VARCHAR(100)); -- SQL should always return zero rows affected
 END
ELSE
 BEGIN
 PRINT '[Status] rows affected by MERGE: ' + CAST(@mergeCount AS VARCHAR(100));
 END
GO
 
SET IDENTITY_INSERT [Status] OFF
GO
SET NOCOUNT OFF
GO

Table definition

CREATE TABLE [dbo].[Status](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](255) NOT NULL,
 CONSTRAINT [PK_Status] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [UK_Status_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

Sample Data in Status Table

The sample data below is the actual data and it appeared in the Results tab of SSMS, but I thought it should appear in the output script in the VALUES section.

  (-1,'a')
 ,(0,'b')
 ,(1,'c')
 ,(2,'d')
 ,(3,'e')
 ,(4,'f')
 ,(5,'g')
 ,(6,'h')
 ,(7,'i')
 ,(8,'j')
 ,(9,'k')
 ,(10,'l')
 ,(11,'m')
 ,(12,'n')
 ,(13,'o')
 ,(14,'p')

@include_values not a parameter?

I thought that @include_values would be needed, but its default is 1. I was surprised to see that when I tried to use:
sp_generate_merge @table_name = 'Status', @include_values = 1 that the following error occurred: @include_values is not a parameter for procedure sp_generate_merge.

Version

Microsoft SQL Server 2019 (RTM-CU15) (KB5008996) - 15.0.4198.2 (X64) Jan 12 2022 22:30:08 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro for Workstations 10.0 (Build 19044: ) (Hypervisor)

UPDATE

I removed the procedure from master into my user database and the issues went away.

I tried reproducing this with your table/data on SQL 15.0.4236.7 but was unable to do so.

The @include_values param was added before SQL 2019, so it may be worth installing the latest version to rule that out.

Other possibilities I can think of: a bug associated with the default value for QUOTED_IDENTIFIER, ANSI_NULLS and ANSI_PADDING. If you could script out the CREATE DATABASE statement for your master DB (e.g. using SSMS), I'll try reproducing it again. Thanks!

Closing as I was unable to reproduce and a workaround is available