Visual Studio thinks correct SQL syntax is incorrect - tsql

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).

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.

Old thread but this did catch me out so I thought I'd share my experience, the documentation for ALTER ROLE seems to indicate that it is supported on SQL 2008 when in fact only part of the functionality is. You can use Alter Role to change the name of a Role i.e. ALTER ROLE [ROLE NAME] WITH [NEW ROLE NAME]
But to add or remove members from the role it is necessary to use the old (and now deprecated) stored procedure sp_addrolememeber i.e. sp_addrolemember 'Role Name', 'User Name'.
As Aaroninus points out if you target SQL 2012 or later this will not report as an error.

In my case I was adding already existing post deployment script(.sql file) by clicking on a folder named "Reference Data" ==>Add Existing Item==>Select List of .sql files including the Script.PostDeployment1.sql file. On pressing F5 it was giving following error:
SQL46010: Incorrect syntax near :.
contents of sql file were :
:r ".\Customer.data.sql"
GO
:r ".\Product.data.sql"
GO
:r ".\OrderType.data.sql"
GO
:r ".\Orders.data.sql"
GO
Solution:
Post Deployment script should be created using VS.In existing project,open Visual studio and add post deployment script by clicking on : Add==>New Item ==>Script ==>Post Deployment Script ==>.PostDeployment1.sql.After creating PostDeployment1.sql file simply copy paste your content an then press F5..PostDeployment1.sql should execute correctly without any error.

having to set a flag in the project is really stupid since i've got ddl sql scripts in both oracle and mssql inside my c# class project.

Related

SSDT Error on build: Expected XML but encountered JSON instead

I'm using the very latest version of Visual Studio Professional 2017 (v15.9.9) with the latest SQL Server Data Tools (v15.1.61903.01040)
I create a new stored procedure in my database project, for example, with the following code:
create proc [dbo].[MyProc] as
select 'Test' as [TestColumn] FOR JSON AUTO
It won't compile with the errors below, and the keyword JSON is highlighted:
SQL46005: Expected XML but encountered JSON instead
SQL80001: Incorrect syntax near 'JSON'
It seems strange that this doesn't compile, when it's perfectly valid SQL (or am I mistaken? Is there something I'm missing? Or is it genuinely not supported?)
This issue seemed to exist for older versions of SSDT before 2016, but was reported as resolved back then, is this perhaps a recent regression? I can't find any other instances of this error or anything like it.
If you have the "Target Platform" on the .sqlproj properties set to SQL 2014 or less or "Microsoft Azure SQL Database" then this will happen, the compatibility level doesn't affect it (or at least setting my project to 2014 compatibility didn't).
This works for me with SQL 2016+ and Azure v12 with SSDT "15.1.61902.21100"
Hope it helps!
ed

The Type spatial_geometry is not qualified with a namespace or alias. Only primitive types can be used without qualification

We've got an existing solution that uses EF5 and DevArt's dotConnect for PostgreSQL to connect to a PostgreSQL 9.4 database, and generate the entities.
I just installed Visual Studio 2013, and the dotConnect for PostgreSQL provider, and pulled the repository. My colleagues have the same setup, which works on their machines.
When I tried to make a build, I got the error message described in the title. I have to open the .edmx file in XML view (because VS can't show the diagram since it contains errors), and every line containing spatial_geometry as type has blue squiggly lines:
When I remove the offending lines, the project builds, but then I'm missing the spatial geometry properties, which are kinda essential to the application. I can even regenerate the entities from the database, but then I get errors that the name is not valid for each of those same properties, and the properties are then simply missing.
Why does Visual Studio complain about that spatial_geometry type (and why does it do so on my machine but not on others')? Where does the conversion between spatial_geomerty and System.Data.Spatial.DbGeometry take place?
Try upgrading to the newest (7.11.1190) build of dotConnect for PostgreSQL. Does this fix the issue? If you do not have access to the licensed v7.11.1190, please check the same with a trial.

How do I change an entity framework project to use Microsoft SQL Server

I am trying to debug a program using Entity Framework code first on my personal (work) computer.
We have recently had a domain migration, meaning that the user I log in as now is not the same that I used before. This caused me to loose access to the databases I had on the computer. To get around this, I have uninstalled everything to do with Microsoft SQL Server on the computer, and installed the latest version of Microsoft SQL Server, 2014 - 12.0.4213.0 . I then restored the database I need.
When I first tried to run the program, Visual Studio complained that the project is set up to use SQL Server Express, which was not installed. The recommended solution is to change the project to use SQL Server instead. To do this, I must click on "the database file" and follow the instructions. I have looked through the entire solution. There is a great many files, but I found no good candidate for "the database file."
It seems that my Google fu is not strong enough to find anything about this. So my question is: how do I change the project to use SQL Server?
I also have a second, related question. I tried to solve the problem by installing SQL Server Express. However, when I try to restore the database to this, no base appears in the drop down list. When I try to run the program now, I get another error:
Unable to create the file 'c:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Timelønsblanket.mdf' because it already exists.
I guess that this is also why I cannot restore the database. What I have found in websearches warns that I should not manually delete .mdf files.
Any advice on what to do?
I have solved the problem. All that was needed was a correct connection string. No need to find a "database file".

Update Target Button is disabled in Sql Server Database Schema Comparison

I use Visual Studio 2013 Professional Update 4 in combination with multiple SQL Server 2012 Enterprise servers/instances. The Idea was to create a Version controlled deployment using a Visual Studio Solution with SQL Server Projects.
However for some reason the "Update Target" and "Generate Script" button is disabled.
I can update the local database project from the sql server database but the other way around is not working. (see screenshot)
Google is not helpful at all although I found a few people with a similar problem.
Update target button is disabled after schema comparison
VS2012: Schema compare, update button disabled
VS2012 Schema compare buttons disabled
But none of this questions/blogposts is about Visual Studio 2013 and in addition none of the proposed solutions is working for me.
Question: Why is the Update Target button disabled and what do I have to change to fix this?
Note: Of course, I'm using the latest version of SQL Server Data Tools(12.0.50318.0)
This was driving me nuts, as well. I finally noticed a "warning" message stating "Cannot generate deployment plan due to an internal error". Because of this, the Update and Generate Script buttons were both disabled.
I resolved this by closing Visual Studio, navigating to the folder containing the Database project and removing all (*.dbmdl) files. I then restarted Visual Studio, re-ran the compare schema and the Update button was enabled.
The tip that helped me was to look at the error list. (View Menu, Error List). Once I cleared the errors (just the errors, the ones with the red X) I was able to compare the schema again and the Update and Generate Scripts buttons were both enabled.
Beside the generate script, click on OPTIONS and select only the objects for which you want to compare.
This and ONLY this worked for me.

System.Resources.MissingManifestResourceException when Updating database

I moved a web application I am working on from one machine to another. It is built using .Net MVC and Entity Framework but when I execute the Update-Database command so that the database is updated, I get this error:
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "NameofMigration.resources" was correctly embedded or linked into assembly "NameofProject" at compile time, or that all the satellite assemblies required are loadable and fully signed.
Does anyone know how to fix this error?
I had a similar issue when the resx part of the migration was not included in the project file when a fellow developer checked the project in (probably due to a merge issue). You may find that the resx file is there but greyed out. If it's there, try right clicking the "NameofMigration.resx" file and selecting "include in project". If it's not there, you better go find it on the other machine and add it to the project :-)
I think the issue (one issue) is that the .resx file is added as "dependent upon" (nested under) the .cs file, and the way the build engine works, "dependent upon" changes the name that an embedded resource is saved with (something like, it changes from being based on the filename to being based on the type name; I've dealt with this in other scenarios but can't remember for sure).
This leads to problems when using SDK .csproj files, for some reason (I guess that by default SDK .csproj does not change the resource name in this situation, but the migrations system expects it to).
As someone else had posted, SDK .csproj can use the following tag to change the embedded resource naming scheme for "dependent upon" resources, which then allows the migrations system to find the embedded resource:
<EmbeddedResourceUseDependentUponConvention>
true
</EmbeddedResourceUseDependentUponConvention>
This should go in a <PropertyGroup> of your SDK .csproj file.
For VS 2017, the solution is as follows:
Go to the project file, and for all of the migrations, apply the following format:
<Compile Include="Migrations\201804251606403_emailsWithEffort.cs" />
<Compile Include="Migrations\201804251606403_emailsWithEffort.Designer.cs">
<DependentUpon>201804251606403_emailsWithEffort.cs</DependentUpon>
</Compile>
<EmbeddedResource Include="Migrations\201804251606403_emailsWithEffort.resx">
<DependentUpon>201804251606403_emailsWithEffort.cs</DependentUpon>
</EmbeddedResource>
I guess that the problem is when changing version(s) of Visual Studio, old format of describing dependencies stays, and the Visual Studio 2017 can not interpret it correctly.
Hence, applying the format as described above (change your format to this), you can make the Visual Studio get the idea of where it's resources are.
Slightly different situation, where I created a new environment, and database, and received the above error message.
For my fix, I had to right-click on the migration files (initial and resx) and set property to embedded as resource. Update-database command ran fine afterward.
I encountered the same issue (VS 2017) and none of the solutions provided here worked. I fixed the problem by cleaning the solution and manually deleting the bin folder and then building it again.
If anybody wants to look into the source or compiler to know why this is happening; I don't feel like it right now. After an hour of tinkering, my resolution is odd.
Granted, I shouldn't have done this in the first place, but for quick code I temporarily added classes into the same file as my generated DbMigration 201906212110305_initial.cs. The mere existence of those temporary classes in the same file caused this error. As soon as I moved them to their own file (which I was going to do all along anyway) the runtime error vanished.
Unloading and then reloading migration file worked for me!