Scalar function parses and runs in SSMS, incorrect syntax in ScriptDOM
1
vote
0
answers
332
views
I'm preparing the Database migration from compatibility level 140 (SQL Server 2017) to level 150 (SQL Server 2019).
As part of the preparation, Data Migration Assistant (DMA) has been run and it found a syntax error in one of the scalar functions.
CREATE OR ALTER FUNCTION dbo.Trim(@String nvarchar(MAX), @NullIfEmpty tinyint)
RETURNS nvarchar(MAX)
AS
BEGIN
RETURN NULLIF(TRIM(CHAR(32) + CHAR(160) + CHAR(10) + CHAR(13) + CHAR(9) FROM @String), IIF(@NullIfEmpty = 1, '', NULL))
END
I can parse and run this function in both compatibility levels 140 and 150 without issues.
But when I've tried parsing it with a ScriptDOM (tutorial by Mala Mahadevan ) or other tools that I assume are using the ScriptDOM (SQL Prompt formatted, Data migration assistant) I've got the same error.
**PowerShell:**
1 parsing error(s): {
"Number": 46010,
"Offset": 123,
"Line": 5,
"Column": 10,
"Message": "Incorrect syntax near NULLIF."
}
**Data Migration Assistant:**
{
"Name": "Dtatest",
"Databases": [
{
"ServerName": "MyServer",
"Name": "DtaTest",
"CompatibilityLevel": "CompatLevel140",
"SizeMB": 16.0,
"Status": "Completed",
"ServerVersion": "15.0.2000.5",
"AssessmentRecommendations": [
{
"CompatibilityLevel": "CompatLevel140",
"Category": "Compatibility",
"Severity": "Error",
"ChangeCategory": "BreakingChange",
"RuleId": "Microsoft.Rules.Data.Upgrade.UR00001",
"Title": "Syntax issue on the source server",
"Impact": "While parsing the schema on the source database, one or more syntax issues were found. Syntax issues on the source database indicate that some objects contain unsupported syntax due to which all assessment rules were not run on the object.",
"Recommendation": "Review the list of objects and issues reported, fix the syntax errors, and re-run assessment before migrating this database.",
"MoreInfo": "",
"ImpactedObjects": [
{
"Name": "[dbo].[Trim]",
"ObjectType": "Object",
"ImpactDetail": "Object [dbo].[Trim] has syntax errors. Incorrect syntax near NULLIF.. Error number 46010. For more details, please see: Line 5, Column 10.",
"SuggestedFixes": []
}
]
},
{
"CompatibilityLevel": "CompatLevel150",
"Category": "Compatibility",
"Severity": "Error",
"ChangeCategory": "BreakingChange",
"RuleId": "Microsoft.Rules.Data.Upgrade.UR00001",
"Title": "Syntax issue on the source server",
"Impact": "While parsing the schema on the source database, one or more syntax issues were found. Syntax issues on the source database indicate that some objects contain unsupported syntax due to which all assessment rules were not run on the object.",
"Recommendation": "Review the list of objects and issues reported, fix the syntax errors, and re-run assessment before migrating this database.",
"MoreInfo": "",
"ImpactedObjects": [
{
"Name": "[dbo].[Trim]",
"ObjectType": "Object",
"ImpactDetail": "Object [dbo].[Trim] has syntax errors. Incorrect syntax near NULLIF.. Error number 46010. For more details, please see: Line 5, Column 10.",
"SuggestedFixes": []
}
]
}
],
"ServerEdition": "Developer Edition (64-bit)"
}
],
"ServerInstances": [
{
"ServerName": "localhost",
"Version": "15.0.2000.5",
"Status": "Completed",
"AssessmentRecommendations": []
}
],
"SourcePlatform": "SqlOnPrem",
"Status": "Completed",
"StartedOn": "2022-02-02T07:34:30.8110417+00:00",
"EndedOn": "2022-02-02T07:34:42.9071256+00:00",
"EvaluateFeatureRecommendations": false,
"EvaluateCompatibilityIssues": true,
"EvaluateFeatureParity": false,
"TargetPlatform": "SqlServerWindows2019",
"DmaVersion": {}
}
**SQL Prompt:**
If it makes any difference, the function is marked as *is_inlineable* in the *sys.sql_modules* DMV.
Until now I thought SQL Engine uses ScriptDOM as well, but I'm getting different results. Any idea why?
**The whole repro script for the DMA:**
CREATE DATABASE DtaTest
ALTER DATABASE DtaTest
SET COMPATIBILITY_LEVEL = 140
GO
USE DtaTest
GO
CREATE OR ALTER FUNCTION dbo.Trim(@String NVARCHAR(MAX), @NullIfEmpty TINYINT)
RETURNS NVARCHAR(MAX)
AS
BEGIN
RETURN NULLIF(TRIM(CHAR(32) + CHAR(160) + CHAR(10) + CHAR(13) + CHAR(9) FROM @String), IIF(@NullIfEmpty = 1, '', NULL))
END
GO
CREATE TABLE dbo.DummyTable
(
Id tinyint PRIMARY KEY
)

Asked by Zikato
(5724 rep)
Feb 2, 2022, 07:54 AM
Last activity: Feb 2, 2022, 08:03 AM
Last activity: Feb 2, 2022, 08:03 AM