Generating SQL change scripts in SSMS 2008 - sql-server

I have gone through many related SO threads and got some basic info.
Already generated DB diagram.
After that i am unable to find a button/option to generate SQL scripts (create) for all the tables in diagram.
"Generate script" button is disabled, even on clicking the table in diagram.
However i enabled the auto-generate option in tools->designer. But what to do with previous diagrams.
I just want an easy way to auto-generate such scripts (create/alter) and would be gud if i get auto-generated stored procs for insert/selects/update etc.
EDIT: I could do generate scripts for DB objects.
Now:
1. How to import DB diagram from another DB.
2. How to generate (and manage their change integrated with VS source control) routine stored-procs like insert, update and select.
Ok let me ask another way, can experts guide on the usual flow of creating/altering tables (across releases), creating stored-procs (are stored-procs the best way to go ?) and their change-management using SSMS design tools and minimal effort ?

You can go to the Object Explorer in SSMS, and right-click on your own database, and then pick "Tasks" > "Generate Scripts" to generate a whole bunch of scripts for your database.
Mind you - this is just a single set of CREATE statements, basically.
If you're using the visual table designer to modify your tables, you can also have it create a script to handle the changes you've made.
And in Visual Studio 2010 Professional or up, you can also take snapshots of databases, and compare two sets of your database and generate ALTER scripts from those .

Related

How to restore / copy data from particular tables in the correct relationship order in SQL Server?

In SQL Server 2012 or greater, what is the best “free or almost free” way to copy the data from a set of tables to another where you can overwrite all the destination data but relationships are present so the copy needs to occur in a specific order?
SSIS does not seem to have a way to accomplish this where it could figure out the relationships in the data and delete/copy the data in the correct order when FKs and relationships are present?
Basically I’m trying to copy the data for Table A, B, C, D, E, etc. which may be related to each other and say take all the data from the source and try to overwrite and delete the data in the destination.
I think existing SQL 2012 functionality will accomplish your needs nicely. You need to use the **Generate and Publish Scripts Wizard** is SQL Server Management
Studio (SSMS) to generate a script you can re-use in SSIS.
1. In Object Explorer, expand Databases, right-click a database, point to Tasks, and then click Generate and Publish Scripts. Follow the steps in the wizard to script the database objects for publishing.
2. On the Choose Objects page, select the objects to be published.
3. On the Set Scripting Options page, select Save scripts to a specific location.
a. To specify advanced publishing options, select the Advanced button.
i. Script DROP and CREATE: True
ii. Types of Data to script: Schema and Data
b. On the Summary page, review your selections. Click Previous to change your selections. Click Next to publish the objects you selected.
4. On the Save or Publish Scripts page review to progress of the operation.
You can use the developed script for your SSIS process.

SQL Server Mangement Studio Alter Script (2005)

Is it possible to generated ALTER sql script from the changes you perform in Server Studio. This is possible in different tools for mysql, basically you get the log of executed statements. Is similar thing possible here, or how do you go about finding out what the modification (ALTER) script is?
Thanks.
-- MB
You will be able to auto generate change scripts in SSMS. Take a look at this article, I believe it might be what you are looking for. http://www.mssqltips.com/tip.asp?tip=1723
from the article
As a part of my best practices, I always save the T-SQL scripts used for creation and modification of objects in SQL Server. When creating and modifying tables using SQL Server Management Studio designer it is easy to right click in the designer and select "Generate Change Script...", but is there a way to automatically script the creation and/or modification of tables made through of SQL Server Management Studio (SSMS) designer? This tip shows you an option that exists within SSMS to automatically generate these scripts for all table changes when using the table designer.
If you're altering a table (by right-clicking and selecting Design, then there is a button named Generate Change Script on the Table Designer toolbar.
If you're talking about changes made in a properties window, there is a script button at the top of each of those that can generate scripts for changes you make through the UI.
Not sure about SQL Server 2005, but in SQL Server Management Studio 2008 and later, you can definitely do this.
When you modify a table in the table designer (right-click on a table in your Object Explorer and pick "Design" from the menu), you can have SSMS generate the ALTER script for you by right-clicking in the designer and choosing "Generate Change Script":

how to handle db schema updates when using schemabinding and updating often

I'm using a MS SQL Server db and use plenty of views (for use with an O/R mapper). A little annoyance is that I'd like to
use schema binding
update with scripts (to deploy on servers and put in a source control system)
but run into the issue that whenever I want to e.g. add a column to a table, I have to first drop all views that reference that table, update the table, and then recreate the views, even if the views wouldn't need to be updated otherwise. This makes my update scripts a lot longer and also, looking the diffs in the source control system, it is harder to see what the actual relevant change was.
Is there a better way to handle this?
I need to still be able to use simple and source-controllable sql updates. A code generator like is included in SQL Server Management Studio would be helpful, but I had issues with SQL Server Management Studio in that it tends to create code that does not specify the names for some indices or (default) constraints. But I want to have identical dbs when I run my scripts on different systems, including the names of all contraints etc, so that I don't have to jump through loops when updating those constraints later.
So perhaps a smarter SQL code generator would a solution?
My workflow now is:
type the alter table statement in query editor
check if I get an error statement like "cannot ALTER 'XXX' because it is being referenced by object 'YYY'."
use SQL Server Managment Studio to script me create code for the referenced object
insert a drop statement before the alter statement and create statement after
check if the drop statement creates error and repeat
this annoys me, but perhaps I simply have to live with it if I want to continue using schemabinding and script updates...
You can at least eliminate the "check if I get an error" step by querying a few dynamic managment functions and system views to find your dependencies. This article gives a decent explanation of how to do that. Beyond that, I think you're right, you can't have your cake and eat it too with schema-binding.
Also keep in mind that dropping/creating views will cause you to lose any permissions that were granted on those objects, so those permissions should be included in your scripts as well.

Can Linq to Sql (L2S) update a database when a class/property is added to the DBML?

I have a small L2S database that was generated with the L2S CreateDatabase command. When I add a class or property to the DBML, the database is not updated automatically. I get errors like "Table not found" at runtime.
Is there a way to add new tables and columns to a database?
No, LINQ to SQL cannot update your database schema to reflect changes made to DBML. To do so you'll have to either recreate your DB from scratch (which, granted, LINQ to SQL does not do very well), or use something like Wizardby to version and upgrade your DB schema.
No it is not. You need to refresh your Server Explorer, and then remove your changed table form DBML, then drag and drop it and CTRL + S :}
Not out of the box, but there are third party tools that add that kind of functionality. Take a look at my tools - Huagati DBML/EDMX Tools.
The add-in can generate the change scripts for you, and there is also a runtime component that can be used within your app to detect and generate SQL-DDL for diffs between the model and the database.

Copy table to a different database on a different SQL Server

I would like to copy a table from one database to another. I know you can easily do the following if the databases are on the same SQL Server.
SELECT * INTO NewTable FROM existingdb.dbo.existingtable;
Is there any easy way to do this if the databases are on two different SQL Servers, without having to loop through every record in the original table and insert it into the new table?
Also, this needs to be done in code, outside of SQL Server Management Studio.
Yes. add a linked server entry, and use select into using the four part db object naming convention.
Example:
SELECT * INTO targetTable
FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
If it’s only copying tables then linked servers will work fine or creating scripts but if secondary table already contains some data then I’d suggest using some third party comparison tool.
I’m using Apex Diff but there are also a lot of other tools out there such as those from Red Gate or Dev Art...
Third party tools are not necessary of course and you can do everything natively it’s just more convenient. Even if you’re on a tight budget you can use these in trial mode to get things done….
Here is a good thread on similar topic with a lot more examples on how to do this in pure sql.
SQL Server(2012) provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
Using the SQL Server Management Studio, right-click on the source database from the object explorer, then from Tasks choose Generate Scripts.
In the Choose objects window, choose Select Specific Database Objects to specify the tables that you will generate script for, then choose the tables by ticking beside each one of it. Click Next.
In the Set Scripting Options window, specify the path where you will save the generated script file, and click Advanced.
From the appeared Advanced Scripting Options window, specify Schema and Data as Types of Data to Script. You can decide from here if you want to script the indexes and keys in your tables. Click OK.
Getting back to the Advanced Scripting Options window, click Next.
Review the Summary window and click Next.
You can monitor the progress from the Save or Publish Scripts window. If there is no error click Finish and you will find the script file in the specified path.
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
Microsoft SQL Server Database Publishing Wizard will generate all the necessary insert statements, and optionally schema information as well if you need that:
http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A
Generate the scripts?
Generate a script to create the table then generate a script to insert the data.
check-out SP_ Genereate_Inserts for generating the data insert script.
Create the database, with Script Database as... CREATE To
Within SSMS on the source server, use the export wizard with the destination server database as the destination.
Source instance > YourDatabase > Tasks > Export data
Data Soure = SQL Server Native Client
Validate/enter Server & Database
Destination = SQL Server Native Client
Validate/enter Server & Database
Follow through wizard

Resources