I'm using sqlpackage.exe to generate differential script for update between different database versions (database projects, .dacpac files), so I'm not running it against existing Sql Server database instance. The problem is that in source project in one table I'm having a column with a default constraint without a name. I'm not changing those constraints nor the columns having those ones. I'm changing the table name, and I remove one column and add new one with different name and type.
What the sqlpackage is generating are, of course, command to rename the code, commands to drop those defaults (those are wrong), and small part of commands removing and adding the columns by creating new table and moving data to the new one and removing old table (but that is OK and out of scope of that problem).
So the changes looks more less like that:
Source table (key def ommitted):
CREATE TABLE SourceTable (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Column1] NVARCHAR (50) NOT NULL,
[Column2] XML NULL,
[Column3] INT DEFAULT ((0)) NOT NULL,
[Column4] VARCHAR (50) DEFAULT ('') NOT NULL
);
Target Table (key def ommited)
CREATE TABLE TargetTable (
[Id] INT IDENTITY (1, 1) NOT NULL,
[OtherColumn] INT NOT NULL,
[Column2] XML NULL,
[Column3] INT DEFAULT ((0)) NOT NULL,
[Column4] VARCHAR (50) DEFAULT ('') NOT NULL
);
On the [OtherColumn] would be created a foreign key.
The generated script is as fallows (removed unnecessary code):
EXECUTE sp_rename @objname = N'[SourceTable]', @newname = N'TargetTable', @objtype = N'OBJECT';
ALTER TABLE [dbo].[TargetTable DROP CONSTRAINT ; <--fails
ALTER TABLE [dbo].[TargetTable] DROP CONSTRAINT ; <--fails
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SET XACT_ABORT ON;
CREATE TABLE [dbo].[tmp_ms_xx_TargetTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[OtherColumn] INT NOT NULL,
[Column2] XML NULL,
[Column3] INT DEFAULT ((0)) NOT NULL,
[Column4] VARCHAR (50) DEFAULT ('') NOT NULL
CONSTRAINT [tmp_ms_xx_constraint_PK_TargetTable] PRIMARY KEY CLUSTERED ([Id] ASC)
);
ALTER TABLE [dbo].[tmp_ms_xx_TargetTable]
ADD CONSTRAINT [SD_TargetTable_6f675e5ba33c4641889f7ad1c05f3be1] DEFAULT 0 FOR [TaskTypeID];
IF EXISTS (SELECT TOP 1 1
FROM [dbo].[TargetTable])
BEGIN
SET IDENTITY_INSERT [dbo].[tmp_ms_xx_TargetTable] ON;
INSERT INTO [dbo].[tmp_ms_xx_TargetTable] ([Id], [Column2], [Column3], [Column4])
SELECT [Id],
[SchedulerID],
[Issuer],
[Details],
[TaskMessage],
[Duration],
[StopMessage]
FROM [dbo].[TargetTable]
ORDER BY [Id] ASC;
SET IDENTITY_INSERT [dbo].[tmp_ms_xx_TargetTable] OFF;
END
ALTER TABLE [dbo].[tmp_ms_xx_TargetTable] DROP CONSTRAINT [SD_TargetTable_6f675e5ba33c4641889f7ad1c05f3be1];
DROP TABLE [dbo].[TargetTable];
EXECUTE sp_rename N'[dbo].[tmp_ms_xx_TargetTable]', N'TargetTable';
EXECUTE sp_rename N'[dbo].[tmp_ms_xx_constraint_PK_TargetTable]', N'PK_TargetTable', N'OBJECT';
COMMIT TRANSACTION;
I don't know how to avoid generation of the code:
ALTER TABLE [dbo].[TargetTable DROP CONSTRAINT ;
Is it some bug? I know that the default constraint in source table should have a name, but now it is to late for that. Any ideas how to solve this?