Hello Zaygum,
Thank you for your question and for providing the detailed error message and query. This is a very common issue in Azure Data Factory (ADF) when dynamically constructing SQL queries, and the "Unclosed quotation mark" error is a classic sign of a string concatenation problem.
You are constructing your SQL query by directly embedding the pipeline parameter into the query string like this: where PlanName='@{pipeline().parameters.PlanName}'.
While this looks correct, the ADF expression builder and the SQL engine can sometimes misinterpret the quotes and newlines, especially when building the final JSON payload for the activity. The error message Incorrect syntax near ' WHERE PlanName= ' strongly suggests that the parameter value and its surrounding quotes are not being rendered correctly into the final SQL command.
The Solution: Use String Concatenation
The most reliable way to fix this is to use ADF's built-in string concatenation function, concat(), to build your query. This method explicitly tells the expression engine how to assemble the string, which avoids any ambiguity with quotes and special characters.
Here is how you should modify your query in the Copy Activity's source settings:
- Go to your Copy Activity in the ADF pipeline.
In the Source tab, instead of typing the query directly into the text box, click on "Add dynamic content".
In the expression builder window that pops up, enter the following expression:
text
@concat('SELECT TOP 10 * FROM dbo.[Master] WITH (NOLOCK) WHERE PlanName = ''', pipeline().parameters.PlanName, '''')
Why This Works
@concat(...): This function explicitly joins multiple string parts together into a single string.
'SELECT ... WHERE PlanName = ''': This is the first part of your SQL query. Notice the three single quotes at the end. The first and last quotes define the string literal in ADF's expression language. The middle quote is an escaped single quote that will be included in the final SQL command.
pipeline().parameters.PlanName: This injects the value of your PlanName parameter (e.g., 'testplan').
'''': This is the final part of the string. It's a single quote that closes the SQL string literal, which itself is enclosed in single quotes for the ADF expression.
When the expression is evaluated, it will correctly build a clean, valid SQL query string like: SELECT TOP 10 * FROM dbo.[Master] WITH (NOLOCK) WHERE PlanName = 'testplan'
This will be passed to the SQL Server, resolving the syntax error and the "unclosed quotation mark" issue. This approach is more robust and is the recommended best practice for building dynamic queries in ADF.
Best Regards,
Jerald Felix