FIX: Snapshot Agent fails when you publish UDF’s to SQL Server 2016 Distributor in Transactional Replication

Applies to: SQL Server 2016 DeveloperSQL Server 2016 EnterpriseSQL Server 2016 Enterprise Core


Assume that you have a setup where Microsoft SQL Server 2016 is acting as Distributor and SQL Server 2014 or SQL Server 2012 is acting as Publisher. In this setup, if you create a snapshot, or a transactional or merge publication on SQL Server 2012 or SQL Server 2014 Publisher with user-defined function (UDF) as an article, then Snapshot Agent fails while scripting UDF, and you receive an error message that resembles the following:

Script failed for UserDefinedFunction 'schema.functionname'

The following error message is seen from the Snapshot Agent job history: 

<DateTime> [5%] The replication agent had encountered an exception
<DateTime> Source: Unknown
<DateTime> Exception Type: Microsoft.SqlServer.Management.Smo.FailedOperationException
<DateTime> Exception Message: Script failed for UserDefinedFunction 'schema.functionname'
<DateTime> Message Code: Not Applicable 


This issue occurs because the Snapshot Agent uses SMO Miscrosft.SqlServer.Smo.dll to script articles. SQL server 2016 adds a new SMO property IsNativelyCompiled for In-Memory functions/stored procedures. This property isn't presented in SQL Server whose version is earlier than SQL Server 2016. When you run the Snapshot Agent on Distributor for SQL Server 2016, it uses Microsoft.SqlServer.Smo.dll of SQL Server 2016, but it tries to check for SMO property IsNativelyCompiled against SQL Server 2012 or 2014 Publisher and it fails with the above error.


This issue is fixed in the following cumulative updates for SQL Server:

Cumulative Update 2 for SQL Server 2016 SP1

Cumulative Update 4 for SQL Server 2016


To work around this issue, create user-defined functions (UDF’s) manually on the Subscriber.


Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.


Learn about the terminology that Microsoft uses to describe software updates.