2

Actually I have two databases, one is for staging one is in production. one table, for example called Class(class_id,descr,faculty_id) and the faculty_id reference to another table called Faculty(faculty_id, name,comment).

my story is that i do so much updates on the staging database, Faculty table, change lots of faculty comment and name without changing Class table. I want to copy the staging Faculty table to Production. My original way is to generate script from staging and delete the data table, Faculty in production and run the script in order to copy the staging data table Faculty to production. However i found i cannot delete the Faculty table because the foreign key. but i do not want to write down hundreds of update statement, and also cannot delete the Class data in production, how can i do if i want to copy the Faculty table to production?

1
  • What version of SQL Server, please. Commented Oct 19, 2012 at 20:17

4 Answers 4

1

Temporarily delete the relation between class and faculty and do whatever and then again establish the relationship..

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Backup your class table that is holding foreign keys.

Truncate the class table.

Make your updates to Faculty.

Finally restore your class table.

2 Comments

Is this a question or an answer? If its a question then plz write it as Comment!!
You can't truncate a table which has a foreign key.. In this case faculty_id in class table..
0

Let's assume that all the surrogate keys in staging Faculty are the same as those in production.

  1. Copy Faculty table to Production as Faculty_Staging
  2. Alter Class table and drop foreign key reference to Faculty
  3. Rename Faculty to Faculty_backup
  4. Rename Faculty_Staging to Faculty
  5. Alter Class table and restore foreign key constraint

If the attempt to rebuild the foreign key reference fails then there are problems with your surrogate keys in your staging Faculty table.

  1. Rename Faculty to Faculty_Staging
  2. Rename Faculty_Backup to Faculty
  3. Alter Class table and restore foreign key constraint.

Things to consider:

  1. Do the surrogate keys in Faculty and Faculty_Staging match exactly?
  2. Do the alternate keys in Faculty and Faculty_Staging match exactly?
  3. Have you inadvertently deleted any production rows in Faculty staging?

Comments

0

Did you know you can Generate a SQL Script with SQL? (This only works if Faculty has same faculty_id in staging and production)

This will gen script to insert the new Faculties:

SELECT 'INSERT INTO production.Faculty (faculty_id, name, comment) 
        SELECT TOP 1 '+LTRIM(faculty_id)+','''+name+''','''+comment+'''
          FROM production.Faculty
         WHERE NOT EXISTS (SELECT 1 FROM production.Faculty WHERE faculty_id = '+LTRIM(faculty_id)+');' AS runMeOnProduction
  FROM staging.Faculty

This will gen script to update the Existing Faculties:

SELECT 'UPDATE production.Faculty SET name = '''+name+''', comment = '''+comment+'''
         WHERE faculty_id = '+LTRIM(faculty_id)+';' AS runMeOnProduction
  FROM staging.Faculty

if you get quotes in you name or comment fields you might want to replace name and comment in the SQL statements above with:

REPLACE(name,'''','''''')
REPLACE(comment,'''','''''')

It's not the best way especially if you get inserts and updates on both tables in both environments. But if the Data for Faculty is only growing in staging this works.

Also some querying software will show a blob or some hex when you run these SQL Statements because it thinks the string is to long to show, if you copy paste it, it might still come out correct. Otherwise try another SQL Query tool. I use DBVisualiser for this kind of thing and it works. I'm not adverting for any particular DB Query tool, it's just I have to query mySQL and MSSQL often so I find myself jumping from App to app in order to get the job done.

If there's Data entry in both environments you should consider using a proper Data Comparing tool : http://en.wikipedia.org/wiki/Comparison_of_database_tools And once you start using these you're question will probably be different.

This is probably wacky thinking but I've found it convenient before...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.