Dacpac to sqlproj - programmatically - database

I am trying to automate the following steps:
We currently try to keep our database project (.sqlproj) up to date with out database, but this is currently a manual procedure.
We open Visual Studio, put our database as source, put our sqlproj as target, select the pieces we want (we only need the tables, views, indexes and stored procedures) and then run a comparison. When that is done, we hit the 'update' button and our sqlproj file gets updated with the files generated or updated. We check this in and done.
Now I'm trying to automate this process. I'm able to generate the source dacpac file with sqlpackage and I can run a comparison script with the dacpac generated using msbuild OR update a target database, but I can't find how to update the sqlproj like the Update button on the above screenshot does.
I've found DACExtensions to try to simulate the behaviour, but it seems the Update button works in its own specific way.
Long story short: what command do I need to run in order to simulate the 'Update' button?
Any and all help would be appreciated :)

Related

How to create database using Database Project in Visual Studio if doesn't exists?

I have a Database project for my personal project and I am trying to deploy my code to my DEV server. I frequently delete and re-create my DEV Server. Right now, DEV Server is newly created with SQL Server. Every time I want to deploy my code I have to manually create Database Project and then publish database project. I want to automate creation of Database with database project deployment.
Right now, I have a script that creates database, but I have to execute it manually. And this is working perfectly but I want to automate this step as well.
Is this even possible? If yes, then how? Please explain step by step. Also what will we mention for Initial Catalog in connection string?
Edit:
I tried to create Database by using
CREATE DATABASE LocalDbTest
in Pre-Deployment Script. But it didn't work. It is creating Database, but then tables are not getting created tables under it. Since I used master database as default database, it is creating table under master. It is not letting me select LocalDbTest database as default because it is not yet created, so I have to select Master as my default database. I tried to Change Database by:
USE LocalDbTest
GO
I used it just after creating Database but this didn't work because when generating script it is changing back to default database. This part is coming automatically when generating script.
USE [$(DatabaseName)];
GO
Also Visual Studio is not letting me add database name in front of table name like:
CREATE TABLE [LocalDbTest].[dbo].[TestTable]
I am getting error:
When you create an object of this type in a database project, the object's name must contain no more than two parts.
If you have a script ready for database creation, you can use the Pre-build event to call SQLCMD and run your script.
Edit:
If you have trouble pointing to a database that does not exist, you may have to manually edit the publish profile (ex. dev.publish.xml) and set the TargetDatabaseName element explicitly. You can also set CreateNewDatabase element to True if you want to be recreated every time it gets published.
Answer:
You can use a publish profile and hardcode the target database in it.

Customise SQL Compare script output for Database Project

We've recently started using Database Projects in Visual Studio 2013. So far, it's great!
There are some things we'd like to be able to customise and I'm not sure if it's even possible.
We'd like to be able to have our post-deployment scripts appended to the SQL that is generated when doing a schema compare, so that when we create a deployment script, it comes with our post-deployment stuff all in the one .sql file. Is there a way to do that?
Also, we'd like to know if there's a way that the .sql file that is generated can be customised (much like a .tt file can be for entity framework).
Welcome to SSDT!
Pre/Post deploy scripts don't get used by the schema compare so if you generate a lot of scripts then set up PowerShell or a batch file to call sqlpackage.exe and set the action to script. I use this as part of a CI build to generate scripts for different environments and works great.
You can customize the output by choosing what to include or exclude in the output script but if there is something not supplied out of the box you can modify it by using deployment contributors which you can add or remove steps or do things like add your own transactions etc, for more details see:
http://blogs.msdn.com/b/ssdt/archive/2013/12/23/dacfx-public-model-tutorial.aspx
If you let us know more about how you want to change it, there may be something already available to help.
Ed

Database project - running Build Action=None SQL scripts

When I choose "Publish..." from a Visual Studio database project, it looks like, among other things, it runs the project's scripts on the database, but only the ones that have Build Action set to "Build". In other words, Build Action "None" scripts don't get run on the database as part of a Publish.
I have a database project but I do not have all the database's items in that project. Can I still keep scripts (for example, for stored procedures) in my project and run them on the database? How?
If I set them to "Build", I get build errors, because the items they depend on are not in the project. If I set them to "None", they don't run on the database at all. Is there a way to make them run on the database without needing their dependent items? If not, what is the best way to do this?
You could try setting the build action to none on the script, and then running the script from a post-deployment script.
There can only be one post-deployment script in each database project. You can add it by adding a new item and searching for post-deployment. Within the script, you use the command syntax to execute scripts.
http://www.mssqltips.com/sqlservertutorial/3006/working-with-pre-and-post-deployment-scripts/
It seems like there is no way to do what I am looking for; that Database projects were meant to have all the dependencies resolved.

Visual studio 2010 sql project - how to run?

I have a SQL project in my .Net project in Visual Studio 2010. I added it to my solution because I thought it will help me to create all the database objects in more environment without difficulties.
My scope is to create/define the database objects (tables and stored procedures) in more computers, easily.For this purpose I created an Sql project in visual studio and I added for each table and stored procedure the creation script in this project. Now I have 30 scripts and I'd like to run it on a new sql instance.
If I open each sql file I have, in visual studio, an toolbat that allows me to tun the opened file on a sql instance (I have an connect button) and this generates the proper object (table or SP).
The question is: how do I do to run all the files in this project in one click. How to create all the objects in one click? Now I have about 30 scripts to run, and I need a proper way.
The way I tried to do it was to set as default project the sql project and to press run (F5), but then I got some strange compilation errors in sql files, errors witch didn't was there when I run (execute) each script individually.
Here is the right click menu for this project (No Run, Publish or something else!):
I tried to use from that menu the Deploy command. The bad news (for me) is that I got this on that command:
I don't have any error in my sql scripts, each one runs correctly.
Thank you.
Use need to deploy your project onto a database thus use the Deploy menu item.
The way database projects work, is that they compare the schema in the project to the schema at the destination.
What this means is you shouldn't have an use statements, or alter statements, ect...
What does one of those procs look like that is throwing the error?

When I save SSIS package the changes are lost

I have a SQL Server 2005 SSIS package with an Execute SQL task. I edit the SQL statement, go through the OKs to make the change. I go back in to the Execute SQL task- and the changes have taken hold. Great.
I now go to save the package. I click save. I look back in the Execute SQL task, and the changes have been lost.
What is going on here and how can I stop it?
Click the Execute SQL Task
Click F4 to see the list of properties
Click the Expressions line and then the little [...] box to see the expressions
Change the expressions (or remove them if you don't want your name/sqlstatementsource/etc. to be set dynamically).
This is a nice feature for reusability and template based development of SSIS packages. We use it allt he time.
I had the same issue. The reason was that I used a config file with all the executables included, and sql code is just a property of one of the executable which is stored in the config file. When you change your code the config file does NOT get updated, but when you close/open your project the values are getting pulled from the config file which reverse your change to the initial state!
In order to fix this you need either to exclude your executable from the config file (what I did) or re-create the config file every time you change the package.
I hope this helps.
I found I was unable to change the "To" in the Send Mail Task. Or rather, I would press OK on the new value and then bring it up again and it would be the old value. What I did -- was to edit the dtsx file in Notepad by searching for the old value and changing it.

Resources