0

I am running a migration script:

--liquibase formatted sql
--changeset endDelimiter:GO
--liquibase formatted sql
--changeset endDelimiter:GO

IF COL_LENGTH('dbo.Account_Details', 'rmCopy') IS NULL
BEGIN
    ALTER TABLE [dbo].[Account_Details]
    ADD [rmCopy] [nvarchar](255),
    [Copy] [varchar](255)
END

IF COL_LENGTH('dbo.Account_Details', 'rmCopy') IS NOT NULL
BEGIN
    UPDATE Account_Details SET rmCopy = 'Target 1 (25)', [Copy] = 'Target 1 (25)' WHERE id = 1;
    UPDATE Account_Details SET rmCopy = 'Target 2 (50)', [Copy] = 'Target 2 (50)' WHERE id = 2;
    UPDATE Account_Details SET rmCopy = 'Target 3 (75)', [Copy] = 'Target 3 (75)' WHERE id = 3;
    UPDATE Account_Details SET rmCopy = 'Target 4 (100)', [Copy] = 'Target 4 (100)' WHERE id = 4;
END

I am getting error like this:

Unexpected error running Liquibase: Invalid column name 'rmCopy'. [Failed SQL: IF COL_LENGTH('dbo.Account_Details', 'rmCopy') IS NULL

Dale K
  • 25,246
  • 15
  • 42
  • 71
Sun
  • 3,444
  • 7
  • 53
  • 83

1 Answers1

0
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[Account_Details]' ) AND name = 'rmCopy')
BEGIN
    ALTER TABLE ADD -- etc...
END
Natrium
  • 30,772
  • 17
  • 59
  • 73
  • I tried it, but it is also giving same issue. But this working in well in test environment. but not working in other environment. but it not showing any permission issue or else in the logs. – Sun Feb 16 '23 at 11:39