1

我正在尝试重命名有触发器的表中的列。我使用 SQL > Refactor > Rename 来重命名列。并且触发文件也得到更新。但是,当我发布时,我得到了这个:

过程 TR_accrual_Accrual_Update,第 134 行无效的列名称“MinHoursRule”。

那是旧的列名。我假设发布首先更新表,并看到当前/旧触发器仍然具有旧列名。

是否可以重命名列、更新触发器和发布?

我能真正想到的唯一解决方案是这样做:

  • 删除触发器并发布
  • 重命名列
  • 再次添加触发器
  • 发布
4

2 回答 2

1

我猜您的发布配置文件设置有问题。您可能禁用了某些东西,例如“不要修改触发器”或类似的东西。我刚刚在 VS 2019 中创建了新的 SSDT 项目,其结构如下:

CREATE TABLE [dbo].[test]
(
    [Id] INT ,
    b int
)

GO

CREATE TRIGGER [dbo].[Trigger_test]
    ON [dbo].[test]
    FOR DELETE, INSERT, UPDATE
    AS
    BEGIN
        SET NoCount ON
        insert into test2 select b from inserted
    END
GO
CREATE TABLE [dbo].[test2]
(
    a int
)
GO

使用默认设置将项目发布到新数据库,并对dbo.test表进行单次插入。确保dbo.test2表中有记录。之后,我重新分解dbo.test.b了专栏,dbo.test.a然后再次发布,一切正常。这是生成的脚本:

/*
Deployment script for trg_test

This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/

GO
SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER ON;

SET NUMERIC_ROUNDABORT OFF;


GO
:setvar DatabaseName "trg_test"
:setvar DefaultFilePrefix "trg_test"
:setvar DefaultDataPath ""
:setvar DefaultLogPath ""

GO
:on error exit
GO
/*
Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF; 
*/
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
    BEGIN
        PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
        SET NOEXEC ON;
    END


GO
USE [$(DatabaseName)];


GO
PRINT N'The following operation was generated from a refactoring log file 80d0e5de-e188-465e-b83c-18f38a1cec98';

PRINT N'Rename [dbo].[test].[b] to a';


GO
EXECUTE sp_rename @objname = N'[dbo].[test].[b]', @newname = N'a', @objtype = N'COLUMN';


GO
PRINT N'Altering Trigger [dbo].[Trigger_test]...';


GO

ALTER TRIGGER [dbo].[Trigger_test]
    ON [dbo].[test]
    FOR DELETE, INSERT, UPDATE
    AS
    BEGIN
        SET NoCount ON
        insert into test2 select a from inserted
    END
GO
-- Refactoring step to update target server with deployed transaction logs

IF OBJECT_ID(N'dbo.__RefactorLog') IS NULL
BEGIN
    CREATE TABLE [dbo].[__RefactorLog] (OperationKey UNIQUEIDENTIFIER NOT NULL PRIMARY KEY)
    EXEC sp_addextendedproperty N'microsoft_database_tools_support', N'refactoring log', N'schema', N'dbo', N'table', N'__RefactorLog'
END
GO
IF NOT EXISTS (SELECT OperationKey FROM [dbo].[__RefactorLog] WHERE OperationKey = '80d0e5de-e188-465e-b83c-18f38a1cec98')
INSERT INTO [dbo].[__RefactorLog] (OperationKey) values ('80d0e5de-e188-465e-b83c-18f38a1cec98')

GO

GO
PRINT N'Update complete.';


GO

于 2021-09-22T12:02:22.443 回答
1

这是我作为解决方法所做的:

  1. 添加新列
  2. 离开旧列
  3. 让触发器使用两组列
  4. 尽快发布/部署到产品
  5. 删除旧列
  6. 稍后发布/部署到产品

因此,我没有重命名,而是创建了新列,然后最终删除了旧列。

呸。但它奏效了。

注意:在我们的 C# 域模型中,我只引用了新列。

于 2021-09-21T21:27:35.117 回答