i have two sites that sell the same products (same database structure and everything). One of them has recently had all its products updated and we need to take the data from three tables on the updated site and override those same three tables on the out of date site. is there a way to do this is Sql Server MS? ive tried truncating the tables in sql to then do a "insert into db1table select * from db2table" but i just get errors from the foreign keys. Someone showed me a trick a while in visual studio enterprise for comparing data where vs spits out a .sql file you can run to update your table but i only have vs professional 2010 and this doesnt seem to be part of the package.
Assuming you have the same foreign key constraints in both the databases and if foreign key constraints are the only issue -
why don't you just
1) Disable all the foreign keys
2) Load the data
3) Enable all the foreign keys
I come from Oracle background, it should not be very hard to disable/enable all foreign key constraints using a script at one go in SQL Server too.
Related
Initial question (solution comes afterwards):
I have the following challenge: I have an Oracle database where a software (Infor Supplier Exchange) once created tables and filled them with data. This db shall be migrated to SQL Server, then an upgrade of the Infor software shall be executed with the migrated data.
A colleague of mine already used a script by Microsoft to migrated the Oracle db to SQL Server which is now available for me. Even though the "Keep Identity" flag was set, no primary key in the new db has its Identiy (autoincrement) set - but that is needed by the Infor software to add data later.
I found a way via SSMS to change the Identity (as well as its seed) for each relevant db table: Right-click on the table, design, change the "Identity Specification" manually. But I have over 300 tables: The effort would cost hours (and sanity).
I also found out that I can use SSMS's "export data" task. You have to know that the Infor software provides a db installer which creates all necessary tables, keys, identity properties, etc. with an EMPTY database. So I can basically export the data from the "Oracle migrated old db" to the "Infor prepared new db" since they (should) have the same table names, keys etc. - except the Identity property and the user data.
In the export data task you can check "Enable identity insert". The problem is that this SSMS feature aborts when it processes a table with foreign keys where the referenced table does not exist, yet. So I could go through the old db again, execute the "copy data" task for all tables without primary keys first, then try the remaining tables until all data is copied to the new db. But this is again much effort since I have to go back on every error or check all contraints beforehand.
Do you have a better approach? Is it possible to copy data from db A (with 300+ tables) to db B (with the same table structure), hoping that a tool solves the correct order of tables because of their foreign key constraints?
If you have questions on the issue I can explain in more detail. Thanks in advance.
Solution:
I solved the task by disabling constraints and triggers temporarily. The steps are:
EXEC sp_MSForEachTable "ALTER TABLE ? NOCHECK CONSTRAINT all"
sp_msforeachtable "ALTER TABLE ? DISABLE TRIGGER all"
EXEC sp_MSForEachTable "DELETE FROM ?"
I had to clear the target database's tables since they are filled with some sample data by the Infor installer. The data export task can append rows or can try to remove existing rows (with same primary keys). But this uses TRUNCATE internally which doesn't work with foreign key contraints, even when they are disabled by the above command.
Next: Execute the SSMS database task "Export data". Ignore datatype conversion errors (some types differ from Oracle-Migration to target SQL schema, like varchar to nvarchar which I checked and judged as not critical).
exec sp_MSForEachTable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
sp_msforeachtable #command1="print '?'", #command2="ALTER TABLE ? ENABLE TRIGGER all"
Using the vendor's SQL Server database schema and loading the data yourself is typically the correct approach for migrating to SQL Server with packaged software. But there may be additional guidance available from the vendor.
Instead of trying to load the tables in an order that is compatible the foreign key constraints, which is not always even possible, disable all the foreign keys before loading the database and re-enable them after. See eg Temporarily disable all foreign key constraints
I need to move one table from one database (used for development) to another (used by user applications/the purpose of the database).
So far I do it with the select or into statement.
That works fine with just one drawback.
When doing this I lose my primary (PK) and foreign keys (FK).
Now I'm handling that by alter the tables after copying them, see the example below, but, it would be beautiful and more effective (and secure copying process) if I could do a copy of the tables with PK and FK intact all the way.
select *
into DB2.dbo.Ta
from DB1.dbo.Ta
alter table DB2.dbo.Ta add primary key (id)
alter table DB2.dbo.Ta add foreign key (Tb_id) references DB2.dbo.Tb
(Where the attribute Tb_id is the PK in table Tb, already existing in the database DB2.)
I work the Microsoft SQL server standard version 2017 and Microsoft SQL server express database engines with Microsoft SQL Studio Management version 2018.
I'm running in to a few strange issues when using database diagrams in Sql Server 2016...
First, I cannot create relationships by dragging and dropping between columns, both in the same and in different tables.
Secondly, when I try to create a foreign key relationship manually in the Database Diagram, or open the database diagram with a table containing a foreign key relationship with another table in the diagram, I receiver a "The Parameter Is Incorrect" error, and it gets stuck trying to load the diagram, while repeatedly generating the error over and over.
I know I've done this before, and I know it's supposed to work... If anyone can alleviate me of my problem, it would be greatly appreciated!
FYI:
Windows 10 Pro
SQL Server 2016
Server hosted on Microsoft Azure
Thanks!
This is a complete shot in the dark but i used to face a similar issue for mySQL - more to do with foreign key relationships. Its possible you may need to drop foreign key constraints in some way whilst you enter the relationship in manually. And then return back to normal. I dont know if there is any setting like this for SQL server 16 but thought it might be something to look at
I'm working on the old C++ MFC project (> 10 years old). Database application works with migrating from MS Access (2007) to MS SQL Server (2008 R2) and I faced some hurdles on the way. For exporting data I used MS SQL Management Studio ("Import" option in the menu)
As it's known, there are some differences in data types between Access and MS SQL. That turned into some troubles.
Columns "ID" from Access (Autonumber, not NULL, primary keys) become just usual columns in SQL Server (int, not NULL and without any autoincrement). So I got lots of mistakes while inserting new rows into the tables.
Yes/No type in Access (-1/0; NULL is not allowed) becomes bit (1/0/NULL), logic of work shouldn't be broken as in the most of the places it is comparision of being not equal to 0:
query.Select()
.Buff("ID", &code)
.FromS("%Table_Name%", NULL)
.Where().Str("Aktiv <> 0")
.Execute();
Looking for a solution I saw the advice to use SSMA (SQL Server Migration Assistant) for Access. It's much better and more intellectual as it recreated primary/foreign keys, created CHECK's, indexes. But unfortunately lots of the FOREIGN KEYs' action Update/Delete operation become not Cascade but No Action. Warning message after schema import:
FOREIGN KEY constraint "Reference77" on MS Access table %Table1% may cause circular or multiple cascade paths. The cascade option from table %Table2% to table %Table1% was set to No option in SQL Server.
And that's not a surprise application gets some errors while deleting objects, though it was all OK in Access. For testing I selected one delete operation (in application) which got errors. I watched error messages and changed No Action -> Cascade for the involved FOREIGN KEYS via SSMS (SQL Server Management Studio). After that delete operation in the application succeeded.
My questions are:
Am I right I need only to change No Action -> Cascade for the FOREIGN KEYs to get the database application can work completely proper? Or there can appear another issues I don't know?
How can it be realized? I would like it to be a good solution for applying it on clients' SQL Servers.
Thanks for help, I really appreciate it!
Thanks for your answer. The solution for my problem is ... exporting data directly from Access (2010) to SQL Server.
I tried:
"SQL Server Import and Export Data", result - copying of only data from Access database, no any primary oк foreign keys, no transformation of autonumber to a column with IDENTITY and autoincrement.
SQL Server Migration Assistant for Access, result - a lot of foreign keys lost CASCADE property for update/delete operations. But all another things are OK.
Access 2010! Database Tools -> SQL Server -> ... using wizard -> all is OK with schema and data. Application works fine with the SQL Server database imported from Access.
So direct export from Access to SQL Server gave the required result.
Probably, but you will still need to test.
For a reusable solution, I would script the database that SSMA created (checking that all the types and foreign keys are correct). Having this script you can create an empty SQL Server database on any number of servers.
To populate these databases I'd use an Integration Services package. It's very easy to create by using Import wizard: going thru all the steps, but saving the package instead of running it immediately. Then you can open this package and edit it (adding data conversions or any other logic if necessary).
I've been using SQL Server for years, so I'm not a noob. I've recently been having problems where I can't add a foreign key to certain tables in my database using SQL Management Studio and SQL Server 2008 Express. The table I'm trying to reference is there and it has a primary key (with the primary key constraint created), but it doesn't show up in the list of tables that I can choose from when I'm trying to add the FK.
If I try and add the FK through plain old T-SQL, the query succeeds, but if I view the new FK using the UI, the primary key table dropdown is empty. The FK is there and it actually does work if I try to insert some data that would violate the constraint.
Anyone know why this would be happening?
This sounds like a tool issue (SSMS), not an engine issue
Thoughts:
close/reopen SSMS (caching?)
patched to same version as server install?
different schema etc?
Edit, after comment and it's SSMS caching:
You can also right-click on the table node and refresh so SSMS updates the cache. This problem goes back to SQL Enterprise Manager and SQL 2000. No known fix after 10 years...