dnlnln / generate-sql-merge

Generate SQL MERGE statements with Table data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DateTimeOffset type is not supported

joakimen opened this issue · comments

When attempting to generate a merge-statement on a table containing a column of the datetimeoffset datatype, the datetimeoffset-type is not recognized, resulting in datetimeoffset-values not being wrapped in single-quotes and generating a faulty script.

Steps to reproduce

CREATE TABLE tbl_datetimeoffset (id int IDENTITY PRIMARY KEY, dto datetimeoffset);
INSERT INTO tbl_datetimeoffset (dto) VALUES (SYSDATETIMEOFFSET());
EXEC sp_generate_merge 'tbl_datetimeoffset';

Result script

--MERGE generated by 'sp_generate_merge' stored procedure, Version 0.93
--Originally by Vyas (http://vyaskn.tripod.com): sp_generate_inserts (build 22)
--Adapted for SQL Server 2008/2012 by Daniel Nolan (http://danere.com)

SET NOCOUNT ON

SET IDENTITY_INSERT [tbl_datetimeoffset] ON

MERGE INTO [tbl_datetimeoffset] AS Target
USING (VALUES
  (1,2018-10-23 13:14:53.7638046 +0)
) AS Source ([id],[dto])
ON (Target.[id] = Source.[id])
WHEN MATCHED AND (
	NULLIF(Source.[dto], Target.[dto]) IS NOT NULL OR NULLIF(Target.[dto], Source.[dto]) IS NOT NULL) THEN
 UPDATE SET
  [dto] = Source.[dto]
WHEN NOT MATCHED BY TARGET THEN
 INSERT([id],[dto])
 VALUES(Source.[id],Source.[dto])
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 [tbl_datetimeoffset]. Rows affected: ' + CAST(@mergeCount AS VARCHAR(100)); -- SQL should always return zero rows affected
 END
ELSE
 BEGIN
 PRINT '[tbl_datetimeoffset] rows affected by MERGE: ' + CAST(@mergeCount AS VARCHAR(100));
 END
GO

SET IDENTITY_INSERT [tbl_datetimeoffset] OFF
GO
SET NOCOUNT OFF
GO

Should probably be handled here:
https://github.com/readyroll/generate-sql-merge/blob/81dacbe11a5f63086052bb2493ba80d23b67975a/master.dbo.sp_generate_merge.sql#L352

Thank you for providing all those details, made it very easy for me to reproduce the issue. I've run the steps against #35 (now merged) and it appears to now be resolved. If you continue to encounter the problem, please let me know.