Hi,
I'm using the Visual Studio Server Explorer to open a table (Table_A) and rearrange the order of fields. When submitting the changed it will generate the script which does the following.
Remove all foregin keys that refer to Table_A and remove it's foreign keys.
Create a temporary table with the new schema. Tmp_Table_A
Copy all data from the old table into the new table.
Delete the old Table.
Rename the new table to "Table_A"
Create all foregin keys on the new table.
I want to know whether everything is done within a transaction. I don't want to lose any of my data.
Does anyone know?
Here is the script.
/* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_User
GO
ALTER TABLE dbo.[User] SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_Customer
GO
ALTER TABLE dbo.Customer SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_CustomerLocation
GO
ALTER TABLE dbo.CustomerLocation SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_Currency
GO
ALTER TABLE dbo.Currency SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_Branch
GO
ALTER TABLE dbo.Branch SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.Tender
DROP CONSTRAINT FK_Tender_CustomerContact
GO
ALTER TABLE dbo.CustomerContact SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
CREATE TABLE dbo.Tmp_Tender
(
TenderID int NOT NULL IDENTITY (1, 1),
CustomerID int NOT NULL,
ContactID int NOT NULL,
LocationID int NOT NULL,
NoteInternal nvarchar(MAX) NULL,
NoteToPrint nvarchar(MAX) NULL,
BranchID int NOT NULL,
CurrencyID int NOT NULL,
ExpiryDate datetime NULL,
CreatedOn datetime NOT NULL,
isInactive bit NOT NULL,
OrderNumber nvarchar(MAX) NULL,
CreatedUserID int NULL
) ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Tender SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_Tender ON
GO
IF EXISTS(SELECT * FROM dbo.Tender)
EXEC('INSERT INTO dbo.Tmp_Tender (TenderID, CustomerID, ContactID, LocationID, NoteInternal, NoteToPrint, BranchID, CurrencyID, ExpiryDate, CreatedOn, isInactive, OrderNumber, CreatedUserID)
SELECT TenderID, CustomerID, ContactID, LocationID, NoteInternal, NoteToPrint, BranchID, CurrencyID, ExpiryDate, CreatedOn, isInactive, OrderNumber, CreatedUserID FROM dbo.Tender WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Tender OFF
GO
ALTER TABLE dbo.TenderItem
DROP CONSTRAINT FK_TenderItem_Tender
GO
ALTER TABLE dbo.LogEntry
DROP CONSTRAINT FK_LogEntry_Tender
GO
DROP TABLE dbo.Tender
GO
EXECUTE sp_rename N'dbo.Tmp_Tender', N'Tender', 'OBJECT'
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
PK_Tender PRIMARY KEY CLUSTERED
(
TenderID
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_CustomerContact FOREIGN KEY
(
ContactID
) REFERENCES dbo.CustomerContact
(
CustomerContactID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_Branch FOREIGN KEY
(
BranchID
) REFERENCES dbo.Branch
(
BranchID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_Currency FOREIGN KEY
(
CurrencyID
) REFERENCES dbo.Currency
(
CurrencyID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_CustomerLocation FOREIGN KEY
(
LocationID
) REFERENCES dbo.CustomerLocation
(
LocationID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_Customer FOREIGN KEY
(
CustomerID
) REFERENCES dbo.Customer
(
CustomerID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.Tender ADD CONSTRAINT
FK_Tender_User FOREIGN KEY
(
CreatedUserID
) REFERENCES dbo.[User]
(
UserID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.LogEntry ADD CONSTRAINT
FK_LogEntry_Tender FOREIGN KEY
(
TenderID
) REFERENCES dbo.Tender
(
TenderID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.LogEntry SET (LOCK_ESCALATION = TABLE)
GO
COMMIT
BEGIN TRANSACTION
GO
ALTER TABLE dbo.TenderItem ADD CONSTRAINT
FK_TenderItem_Tender FOREIGN KEY
(
TenderID
) REFERENCES dbo.Tender
(
TenderID
) ON UPDATE NO ACTION
ON DELETE NO ACTION
GO
ALTER TABLE dbo.TenderItem SET (LOCK_ESCALATION = TABLE)
GO
COMMIT