SQL Compare deployment scripts do not include all ALTER SCHEMA commands - sql-server

I have a database that I am trying to clean up on SQL Server 2008 (not R2). Currently, all tables reside in the dbo schema. Some table names are singular, others are plural.
I created a new schema, crm. I moved all of the tables from dbo to crm and I renamed the singular table names to match the plural table names. When I perform the SQL Compare (version 10.4.8.87) between my development database and production, the script includes the following:
PRINT N'Creating schemata'
GO
CREATE SCHEMA [crm]
AUTHORIZATION [dbo]
GO
...
(removes foreign key constraints, notice it removes them from the plural tables in dbo schema)
PRINT N'Dropping foreign keys from [dbo].[CustomersCommittees]'
GO
ALTER TABLE [dbo].[CustomersCommittees] DROP CONSTRAINT [FK_CustCom_ComID]
ALTER TABLE [dbo].[CustomersCommittees] DROP CONSTRAINT [FK_CustCom_CustID]
GO
...
EXEC sp_rename N'[dbo].[Customer]',N'Customers',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Customers]
EXEC sp_rename N'[dbo].[CustomerAddress]',N'Addresses',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Addresses]
EXEC sp_rename N'[dbo].[Committee]',N'Committees',N'OBJECT'
ALTER SCHEMA [crm] TRANSFER [dbo].[Committees]
...
(adds foreign key contraints back, notice how it adds them to the plural tables in the new crm schema, but never included a statement to ALTER SCHEMA)
PRINT N'Adding foreign keys to [crm].[CustomersCommittees]'
GO
ALTER TABLE [crm].[CustomersCommittees] ADD CONSTRAINT [FK_CustCom_ComID] FOREIGN KEY ([CommitteeID]) REFERENCES [reference].[Committees] ([CommitteeID])
ALTER TABLE [crm].[CustomersCommittees] ADD CONSTRAINT [FK_CustCom_CustID] FOREIGN KEY ([CustomerID]) REFERENCES [crm].[Customers] ([CustomerID])
GO
...
As mentioned above, it does not include any of the ALTER SCHEMA ... TRANSFER ... commands for the tables that were already plural and do not need the sp_rename command to be executed.
Has anyone else seen this?

Maybe I could answer more accurately if I know how SQL Compare handles these other tables... in other words, do further modifications need to be made, do the tables need to be rebuilt, or are they ignored altogether?
For the tables that are being transferred in the way you like, is it because you used schema mapping to force SQL Compare to map dbo to crm?

Related

SQLServer Foreign Key Checks Disabled But Still Can't Drop Table

I have a SQLServer database which I want to drop a table from. The table has FK constraints, but in this case it doesn't matter because when I repopulate the table, the FKs will be replaced correctly.
I've done EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all" which gives a message: sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINTS all" succeded, but when I try to drop my table I get the following SQL error: SQL Error: Could not drop object 'myTable' because ti is referenced by a FOREIGN KEY constraint.
Should the command not have affected my entire database and allowed me to drop the table without an issue? I've also tried doing EXEC sp_msforeachtable "ALTER TABLE myTable NOCHECK CONSTRAINT all" which results in the same error.
NOCheck disable the constraints so they won't be enforced. This would allow you to delete the data without violating the constraint.
Dropping the table would make the constraint definition invalid. You can't have a constraint that references a table that doesn't exist. You wouldn't be able to drop the referenced column from the table either while the constraint exists.
If you will be repopulating the table, just delete the table data and reload it. If you absolutely must drop and recreate the table, you'll need to include the drop & create statements for your foreign key constraints as well.
Note that if you disable the constraint, you'll need to enable using WITH CHECK CHECK (yes twice). The first check turns the constraint on for new data, the second tells SQL to validate the existing data. If you only do one, new data will be checked, but the existing data will not be 'TRUSTED', which can affect how SQL will leverage the FK reference in queries.
Should the command not have affected my entire database and allowed me
to drop the table without an issue?
No. It doesn't matter what the NOCHECK state is; if there are FK constraints that reference a target table, that target table cannot be dropped.
The only way to drop the table is to first drop the FK constraints that reference it.

Remove record from sys.objects?

I ran a ALTER SCHEMA ... on a table (SQL Server 2012 SP1) but the sys.objects record is still there. When I run the ETL I get an error that the table doesn't exist anymore because it's trying to remove all constraints. This one is of type PK Primary Key Constraint. How can I safely remove the record from the sys.objects table?
I managed to fix this issue by scripting the database to a new one, ALTER SCHEMA ... again on that table to bring it back to dbo., then DROP and recreate the table in the different table schema.

SQL Server - ALTER query can create FK relationships, but cannot view them?

I'm stumped on whether I have written the correct syntax to create a foreign key.
I used SQL Server 2012 Express.
If I run a ALTER query to set a foreign key relationship between two table, it works fine, no errors occured. However, if I right-click the table where the FK was created, I don't see any relationships.
This is the ALTER query I have wrote. It creates a relationship between Employers and Employees with EmployerID as a FK.
USE demodemo;
BEGIN TRAN t1
ALTER TABLE Employees
WITH check
ADD CONSTRAINT Employees_EmployerID_FK FOREIGN KEY
(EmployerID) REFERENCES Employers(ID);
GO
The command was executed 'successfully'.
However, if I right click the table, Employees, and select 'Relationships'.
No foreign keys relationships can be seen.
I thought writing the above ALTER query would be the equivalent of creating a FK relationship via the 'Relationships' gui.
Despite having no issues in creating foreign key relationships, I just cannot see them at all.
What could I be doing wrong?
Is my ALTER query correct?
What is the ALTER syntax equivalent to allow me to view the "selected relationships"?
Your DML is missing COMMIT. Also, right click and refresh after executing the SQL
Raj

Invisible Foreign Key in SQL Server 2005

I have a script to update a database by checking for a foreign key's existence and, if it doesn't exist, creating it. It was generated by SQL Management Studio.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_tblChangeRequestForecast_tblCostingCenter]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblChangeRequestForecast]'))
ALTER TABLE [dbo].[tblChangeRequestForecast] WITH CHECK ADD CONSTRAINT [FK_tblChangeRequestForecast_tblCostingCenter] FOREIGN KEY([CostingCenterID])
REFERENCES [dbo].[tblCostingCenter] ([CostingCenterID])
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_tblChangeRequestForecast_tblCostingCenter]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblChangeRequestForecast]'))
ALTER TABLE [dbo].[tblChangeRequestForecast] CHECK CONSTRAINT [FK_tblChangeRequestForecast_tblCostingCenter]
GO
The script raises an error when it's run:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tblChangeRequestForecast_tblCostingCenter". The conflict occurred in database "mydatabase", table "dbo.tblCostingCenter", column 'CostingCenterID'.
This is very mysterious. There is no trace of the foreign key as far as the queries can tell, but the create script fails with the above error. The server is running SQL Server 2005 SP3 (9.00.4035.00).
[Update]: I've just reproduced the issue on a SQL Server 2012 instance. So the version doesn't appear to be too important.
Any idea what could cause this?
It is complaining about the FK constraint that you are trying to add because there is existing data in the table that would no longer be valid if the constraint was successfully applied.
Probably means there is one or more ChangeRequestForecast records where CostingCenterID has a value that doesn't correspond with CostingCenter.CostingCenterID.
Open up SSMS, go to that database, expand that table, and look in the Constraints section. There likely is one in there called "FK_tblChangeRequestForecast_tblCostingCenter" that you need to figure out if you need, and if not, delete it.

Oracle imp tables fail due to restraints

I am using the Oracle imp utility to import data into a set of empty tables in Oracle 10g. When I try to perform the import much of it fails due to referential integrity constraints. E.g. Can't import data into table A because foreign key in table B does not exist yet.
Here is my import command:
imp C_PLUS/<password> rows=y file=db.dmp ignore=y FROMUSER=C_PLUS
TOUSER=C_PLUS tables=...
I could manually go through each table on import and when it fails import the missing table first, but I have over 400 tables that need imported and it would take far too long.
Is there a way around this or does anyone have any ideas?
Thanks
Since you want every table owned by C_PLUS, you can omit the TABLES clause. Just specify the FROMUSER. You can also omit the TOUSER parameter since you're not changing the schema name but that won't affect the behavior of the import. Omitting the TABLES clause should fix the problem with constraints assuming that all the foreign key constraints reference other tables in the same schema and assuming that the data in the export file allows the constraints to be enabled (if the export was done without setting CONSISTENT=Y, there may be orphaned rows in the dump file that will prevent the constraints from being created for example).
For each table you can disable the primary key/foreign key constraints before import. Then enable each primary and foreign key constrains after the import.
Disable contraints
ALTER TABLE table_name DISABLE PRIMARY KEY CASCADE;
Enable constraint
ALTER TABLE table_name ENABLE CONSTRAINT constraint_name;
If the number of table and the number of constraints are too many to do it manually, you can write scripts to generate the ALTER TABLE commands using the data dictionary views.
Disable PK/FK contraints
SELECT 'ALTER TABLE '||table_name||' DISABLE PRIMARY KEY CASCADE' sql_statement
FROM USER_TABLES;
Enable PK constraint
SELECT 'ALTER TABLE '||table_name||' ENABLE CONSTRAINT '||constraint_name sql_statement
FROM USER_CONSTRAINTS
where constraint_type ='P';
Enable FK constraint
SELECT 'ALTER TABLE '||table_name||' ENABLE CONSTRAINT '||constraint_name sql_statement
FROM USER_CONSTRAINTS
where constraint_type ='R';

Resources