SQL Server database project - Create change scripts - sql-server

We started using SQL Server database project in Visual Studio 2012 by connecting an existing database. We are developing a web application that connects with this database.
Now we have around twenty stored procedures updated for the next release. How can I create a script file that contains only the updated stored procedures? We need to send this script to our clients along with the Web application executable files.
Is there any feature that allows us to compare and create only the changed scripts?
Thanks in advance!

Use Schema Compare as follows:
Create a Schema Comparison that compares the database project to one of the production databases.
Run the schema compare.
Exclude everything except for the stored procedures (plus any objects on which they depend).
Click Generate Script.
If you save this Schema Comparison then you can reuse it the next time you need to publish more stored procedure changes.

Related

Cleared SQL Server tables still retain some data

I made a custom application that is running from several years and is full of company data.
Now I need to replicate the application for another customer, so I set up a new server then i cloned the databases and empty all the tables.
Then I made a database and file shrink.
On the SQL Server side, the databases looks empty but if I run a grep search on the database files .mdf and .log I still can find recurrence of the previous company name also in system databases.
How do I really clean a SQL Server database?
Don't use backup/restore to clone a database for distribution to different clients. These commands copy data at the physical page/extent level, which may contain artifacts of deleted data, dropped objects, etc.
The best practice for this need is to create a new database with schema and system data from scratch using T-SQL scripts (ideally source controlled). If you don't already have these scripts, T-SQL scripts for schema/data can be generated from an existing database using the SMO API via .NET code or PowerShell. Here's the first answer I found with a search that uses the Microsoft.SqlServer.Management.SMO.Scripter class. Note you can include scripts data too (insert statements) by specifying the ScriptData scripting option for desired tables.

It is possible to generate SQL script to create only stored procedures for EF mapping?

I have a SQL Server database project with definition of tables, relations, keys, indexes, security, publish profiles, etc. and I also have an ASP.NET Core MVC project.
The issue is that I would like to use Entity Framework for DML operations but the catch is that I am allowed to use only stored procedures (probably from the security perspective, where application account will have only execution permission on those stored procedures and read only on specific tables).
I found that EF can map .SaveChanges() method to stored procedures via model builder like this example
modelBuilder.Entity<SubjectArea>().MapToStoredProcedures();
From the MS docs https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/cud-stored-procedures
It can also generate them with migration command. I am not experienced with migrations, but I would like to only generate the SQL for the stored procedures creation and add this script (which was not run yet) into the SQL Server database project (the workaround is to run migration into separate database and generate scripts from the database into the database project, but it seems not nice).
Is it possible to do it somehow?

Tables and stored procedures not getting included in the generated script

I have created a SQL Server database project using Visual Studio 2015. The underlying database is SQL Server 2016. I have created the project by importing an existing database into the project. I have the structure ready. When I click on publish and select generate, it doesn't include the tables and stored procedures in the generated script. I can see one odd script added. Am I missing something?
Please see the screenshot of my project
The issue has been now resolved. The mistake i was making is importing the data objects from the database and trying to generate the script pointing to the same database. It was not generating the scripts since the database objects existed . Pointing to another database helped me generating the scripts.

Visual Studio Database Schema Compare Adds not showing up

I'm setting up a new SQL Server Database Project for the first time using an existing database. I created my new project, then imported the database and it worked just fine.
However when I add a new stored procedures to my new Database Project and then do a Compare Schema to my local database, the Add of the new stored procedure doesn't show up.
To be clear, when running the Compare I'm setting my source to my Database Project and target to my local database.
The Compare Schema works great for modifications or deletions of stored procedure scripts in my Database Project just not when I define a new one. Am I missing something? Why don't new stored procedures define in my project show up in the comparison results?
I'm using VS2013, SQL Server 2008 R2.
It turns out when I created the stored procedure in Visual Studio I chose the Script... option instead of the Stored Procedure... option. The image below shows what I should have done.
Add the file as a Script resulted in the .sql file being added to a different ItemGroup in the project with no build properties. This is what caused the comparison to not pick it up as a stored procedure.
I determined this by looking at the properties of the .sql file (in Visual Studio) that I added and another .sql in the same project where the comparison worked. The file I added as a Script file had different properties than other files that were being treated as real stored procedures.
Moral of the story... make sure you add stored procedures in Visual Studio correctly.

Create Sql server 2008 Huge database script

I have a about 250 SQL script files included tables, stored procedure, user define function, triggers scripts.
I use these files to create and update old database. i want to enhance the time of creation cause it takes very long time to complete. is there more efficient way to create new database with all needed script indexes, triggers, views, SP, UDF.. or way to speed up the creation time.
Just import the scripts into a New SQL Server Database Project in Visual Studio and do a 'Deploy' of the Database Projects after setting up the connection strings properly.
See:
How to: Import Database Objects from a Script. and
Creating A Sql Server Database Project In Visual Studio 2012..
Are you using SQL Server Data Tools? These tools allow you to point your Visual Studio solution at a DB instance and it will generate you a Diff file, based on your DB projects contents.

Resources