sqldata/sqlsncapi.cpp
// Remove foreign key constraints referencing to the parent table
Remove foreign key constraints referencing to the parent table
Remove foreign key constraints referencing to the parent table
int SqlSncApi::DropReferences(const char* table, size_t *time_spent)
{
	std::string schema;
	std::string name;
	SplitQualifiedName(table, schema, name);
	std::string drop = "BEGIN "\
					   "DECLARE @stmt VARCHAR(300);"\
					   "DECLARE cur CURSOR FOR SELECT 'ALTER TABLE [' + OBJECT_SCHEMA_NAME(parent_object_id) "\
					   " + '].[' + OBJECT_NAME(parent_object_id) + '] DROP CONSTRAINT ' + name " \
					   "FROM sys.foreign_keys WITH (NOLOCK) WHERE ";
	if(schema.empty() == false)
	{
		drop += "OBJECT_SCHEMA_NAME(referenced_object_id) = '";
		drop += schema;
		drop += "' AND ";
	}
	drop += "OBJECT_NAME(referenced_object_id) = '";
	drop += Str::UnquoteIdent(name);
	drop += "';";
    drop += "OPEN cur;"\
			"FETCH cur INTO @stmt;"\
			"WHILE @@FETCH_STATUS = 0 "\
			"BEGIN "\
			" EXEC (@stmt);"\
			" FETCH cur INTO @stmt; "\
			"END "\
			"CLOSE cur; "\
			"DEALLOCATE cur; "\
			"END";
	int rc = ExecuteNonQuery(drop.c_str(), time_spent);
	return rc;
}
int SqlSncApi :: DropReferences ( const char * table , size_t * time_spent ) { std :: string schema ; std :: string name ; SplitQualifiedName ( table , schema , name ) ; std :: string drop = " " " " " " " " " " ; if ( schema . empty ( ) == false ) { drop += " " ; drop += schema ; drop += " " ; } drop += " " ; drop += Str :: UnquoteIdent ( name ) ; drop += " " ; drop += " " " " " " " " " " " " " " " " " " " " ; int rc = ExecuteNonQuery ( drop . c_str ( ) , time_spent ) ; return rc ; }
Remove foreign key constraints referencing to the parent table
Remove foreign key constraints referencing to the parent table
// NOLOCK is used to prevent "[Microsoft][SQL Server Native Client 11.0][SQL Server]Transaction (Process ID 54) was deadlocked on lock resources with  // another process and has been chosen as the deadlock victim. Rerun the transaction."