Track Structure Changes in a database - sql

I am using SQL Server 2012. I want to track the schema changes made in a database. For example, when a new column is added to a table (weather from designer or script), a script should be generated like 'alter table tbl1 add col1 int' and so on.
I got to the Schema Changes History Report but it doesn't provide enough information required as it only shows the table changed, change type and date and time etc but does not provide the script which was generated to make that change.

Well I got to the solution via this link. It is too easy to enable
https://www.mssqltips.com/sqlservertip/1723/auto-generate-change-scripts-in-sql-server-management-studio-ssms-for-tables/

Related

SQL: Tracking changes to the table that gets truncated everyday (and repulled form different srvr)

I have a table that is a replicate of a table from a different server.
Unfortunately I don't have access to the transaction information, and all I have is the table that shows "as is" information & I have a SSIS to replicate the table on my server every day (the table gets truncated, and new information is pulled every night).
Everything has been fine and good, but I want to start tracking what has changed. i.e. I want to know if a new row has been inserted or a value of a column has changed.
Is this something that could be done easily?
I would appreciate any help..
The SQL version is SQL Server 2012 SP1 | Enterprise
If you want to do this for a perticular table then you can go for a scd(slowly changing dimension) transform in SSIS control flow which will keep the hystory records in different table
or
you can create CDC(changing data capture) method on that table.CDC will help you on monitering of every DML operation in that table.It will inserted in the modified row in the system table.

How To Edit .sdf file

I have my application and I install a .sdf file and I add columns to my database. So how do I update my database without losing my Data?
You can execute standard data definition language (DDL) commands in code at application startup.
For example:
myCommand.ExecuteNonQuery("ALTER TABLE MyTable ADD NewColumn1 INT NULL")
We have done this for years in devices ranging from PocketPC to Tablets.
We used to check whether or not a database table or column existed before modifying the DB structure, but we have found it is much easier to record the current database version in a table and then just check the version on startup then make the modifications that we know are necessary between the current version in the database and the application's database version.
You can extend/update a database schema by choosing Default Values for new columns or allow new columns to accept Nulls.

Change Database Column Design in SQL Server

I have a production database which I need to make a change from varchar to nvarchar in the column design. I know I can force it to re-create the design and save it but is it a good idea to do so?
What could be the outcomes and any other suggestions?
Update: Its SQL Server 2008
#Ye Myat Aung, You can directly change the data type from varchar to nvarchar. No need to recreate the structure. Just take care of the size of the column, because nvarchar is saving data in unicodes, it will be almost double to the varchar.
i would suggest to create a test database from the production database and run the script to update the field type.
You should follow the following steps:
Open the table in design view.
Now you change the datatype from varchar to NVarchar at design time and not to save it.
Now you press "Change generate Script" which is available on top left side of header bar.
Save the script and execute it.
This is the most save solution for production servers to avoid any data loss.
If you would be using Visual Studio Database edition to generate the deployment script this would be automatically created for you. However, what it does in behind is as follows
1) Create table with the new definition. Just name your table with {TableName}_new.
2) Create the indexes as you have on the old table
3) Create The triggers as you had on the old table (and disable them)
4) Copy over the data from the old table to new table.
5) Rename the Old Table by appending _old to the table name
6) Rename the new table by dropping the _new from the name

Doctrine schema changes while keeping data?

We're developing a Doctrine backed website using YAML to define our schema. Our schema changes regularly (including fk relations) so we need to do a lot of:
Doctrine::generateModelsFromYaml(APPPATH . 'models/yaml', APPPATH . 'models', array('generateTableClasses' => true));
Doctrine::dropDatabases();
Doctrine::createDatabases();
Doctrine::createTablesFromModels();
We would like to keep existing data and store it back in the re-created database. So I copy the data into a temporary database before the main db is dropped.
How do I get the data from the "old-scheme DB copy" to the "new-scheme DB"? (the new scheme only contains NEW columns, NO COLUMNS ARE REMOVED)
NOTE:
This obviously doesn't work because the column count doesn't match.
SELECT * FROM copy.Table INTO newscheme.Table
This obviously does work, however this is consuming too much time to write for every table:
SELECT old.col, old.col2, old.col3,'somenewdefaultvalue' FROM copy.Table as old INTO newscheme.Table
Have you looked into Migrations? They allow you to alter your database schema in programmatical way. WIthout losing data (unless you remove colums, of course)
How about writing a script (using the Doctrine classes for example) which parses the yaml schema files (both the previous version and the "next" version) and generates the sql scripts to run? It would be a one-time job and not require that much work. The benefit of generating manual migration scripts is that you can easily store them in the version control system and replay version steps later on. If that's not something you need, you can just gather up changes in the code and do it directly through the database driver.
Of course, the more fancy your schema changes becomes, the harder the maintenance will get i.e. column name changes, null to not null etc.

Applying changes easily in Access Database

I have got a backup of a live database (A copy of an ACCDB format Access database) in which I've worked, added new fields to existing tables and whole new tables.
How do I get these changes and apply that fast in the running database?
In MS SQL Server, I'd right-click > Script Table As > Alter To, save the query and run it wherever I desire, is there an as easy way as that to do it in an Access Database ?
Details:
It's an ACCDB MS-Access database created on Access 2007, copied and edited in Access 2007, in which I need to get some "alter" scripts to run on the other database so that it has all the new columns and tables I've created on my copy.
For new tables, just import them from one database into the other. In the "External Data" section of the ribbon, choose the Access icon above "Import". That choice starts an import wizard to allow you to select which objects you want imported. You will have a choice to import just the table structure, or both structure and data.
Remou is right that you can use DDL ALTER TABLE statements to add new columns. However, DDL might not support every feature you want for your new columns. And if you want not just the empty columns added, but also also any data from those new columns, you will probably need to run UPDATE statements to get it into your new columns.
As far as "Script Table As", see if OmBelt's Export Table to SQL tool for MS Access can do what you want.
Edit: Allen Browne has sample ALTER TABLE statements. See CreateFieldDDL and the following one, CreateFieldDDL2.
You can run DDL in Access. I think it would be easiest to run the SQL with VBA, in this case.
There is a product called DbWeigher that can compare Access database schemas and synchronize them. You can get a free trial (30 days). DbWeigher will write a script of all schema differences and write it out as DDL. The script is thorough and includes relationships, indexes, validation rules, allow zero length, etc.
A free tool from the same developer, DBWConsole, will let you execute a DDL script against any Access database. If you wrote your own DDL scripts this would be an easy way to apply the changes to your live database. It even handles some DDL that I don't know how to process in VBA (so it must be magic). DBWConsole is included if you downloaded the trial version of DBWeigher. Be aware that you can't make schema changes to a table in a shared Access database if anyone has the table open.
DbWeigher creates a script of all differences between the two files. It can be a lot to manually parse through if you just want a few of the changes. I built a parser for DbWeigher script files so they could be filtered by table, to extract just the parts I wanted. I contacted the DbWeigher author about it but never heard back. It's safe to say that I have no affiliation with this developer.