Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio thinks correct SQL syntax is incorrect

In Visual Studio 2013, I have created a database project and imported an existing database. At first all was fine, and the project built and produced create scripts.

However, now Visual Studio seems to think that there are SQL syntax errors, returning several SQL46010: Incorrect syntax near: errors. These are all from code generated by VS - I have not written any of it.

Example 1:

ALTER ROLE [db_owner] ADD MEMBER [SomeUser];
SQL46010: Incorrect syntax near ADD.

Example 2:

DECLARE @Foo NVARCHAR(7) = 'abcdefg';
SQL46010: Incorrect syntax near =.

If I copy/paste the code into SSMS, everything works fine.

Unfortunately, these prevent the project from building, which means I can't publish. Since they are errors, not warnings, I cannot set them to be ignored in the project settings.

A workaround exists where I can set the problem files' Build Action to None, but I need these files included when I publish.

I have tried:

  • Deleting and re-adding the same code
  • Copy/pasting code to new SQL file
  • Closing and reopening the solution
  • Closing and reopening Visual Studio
  • Updating Microsoft SQL Server Data Tools (SSDT)
  • Updating Visual Studio

The closest problem I have been able to find is this MSDN thread from 2012, stating that there is a bug SSDT SQL parser (which prompted me to try updating SSDT).

like image 463
Aaroninus Avatar asked Jan 14 '15 17:01

Aaroninus


People also ask

Can Visual Studio run SQL query?

Visual Studio Code is a graphical code editor for Linux, macOS, and Windows. It supports extensions, including the mssql extension for querying a SQL Server instance, Azure SQL Database, an Azure SQL Managed Instance, and a database in Azure Synapse Analytics.

What does Incorrect syntax mean in SQL?

Overview. This SQL error generally means that somewhere in the query, there is invalid syntax. Some common examples: Using a database-specific SQL for the wrong database (eg BigQuery supports DATE_ADD, but Redshift supports DATEADD) Typo in the SQL (missing comma, misspelled word, etc)

Can I use SQL Server in Visual Studio?

SQL Server Data Tools for Visual Studio. Turn Visual Studio into a powerful development environment for SQL Server, Azure SQL Database and Azure SQL Data Warehouse.


1 Answers

There is a setting in the project properties called Target platform that tells VS what version of SQL Server to check the syntax against.

These are relatively new additions to the T-SQL syntax, and they might not work if the syntax was set to e.g. SQL Server 2005.

Visual Studio 2013 database project settings

like image 91
GSerg Avatar answered Sep 28 '22 11:09

GSerg