简体   繁体   中英

VS2010 database project generates SQL03070 error with ALTER statements

I've been struggling for a while with a newly set up Database Project for an SQL 2008 R2 database server in VS2010. I am not a database developer / administrator, but as things often are in IT it ended up on my desk.

I created scripts for tables, views, stored procedures and functions using SSMS => Tasks => Generate Scripts functionality. No matter which options I choose for the script generation, I always end up with some problems, ie as soon as I use the IF NOT EXISTS option I get an SQL03070 error saying "This statement is not recognized in this context." .

I also get the same error when I change CREATE [...] to ALTER [...] . Following a post on Microsoft's Connect Website this is to be expected as "The database project does not allow alters as it will dynamically determine if an ALTER or CREATE is necessary against a specifc database target." Fine with me, but when I choose Execute SQL from the context menu I do get an error if the specific object already exists in the database - I expected the dynamic generation of an ALTER statement in that case.

Unfortunately, due to a lot of legacy code / objects in the database I cannot create or run a full deployment script, so basically the idea is to use VS2010 with TFS to have at least some of the advantages of source control when changing objects in the database.

Always running into error SQL03070 though is not really an option, and importing the database using Import Database Objects and Settings from within VS2010 also does not work as intended as I don't have a single file for eg a table definition (with keys, indexes etc.) but multiple files that are hard to handle.

The last option, setting the Build Action for the scripts to Not in Build results in the inability to run Code Analysis on those files, something we would also like to do.

So basically my question is what would be the best approach for our set up to use database projects / TFS from within VS2010 without getting SQL03070 errors but with code analysis enabled? I do feel quite confused and apologize in case that some aspects of my problem might not be clear enough, please feel free to ask for more details and I will try to answer as good as I can.

Thanks in advance

G.

The idea behind the database projects is that the scripts generate a SQL file when you do a Deploy build (Available from the Build Menu). Before you do this however you need to "Target" a particular database. This can be done through the Deploy Tab within the Project properties. This then gets the Schema of the Target database and then get the schema of your project and generates a script on the differences to make the target database the same as your project. Hopefully this makes sense.

This is a good article that explains this a bit better.

http://blogs.msdn.com/b/gertd/archive/2009/06/05/declarative-database-development.aspx

Best of Luck,

C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM