SQL Server - Rename a column in a ledger enabled table.

Juhi Srivastava 1 Reputation point Microsoft Employee
2021-12-29T14:54:00.66+00:00

Hello Everyone,

I would like to know whether renaming a column is supported in table which has ledger capability.
If yes then please let me know how to achieve it?
otherwise Is there a plan to support this in future?

ref: https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-ledger-column-history-transact-sql?view=azuresqldb-current
The above doc gives a hint that renaming might be supported for the ledger tables.

Thank you in advance

Azure SQL Database
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Phillips 17,776 Reputation points
    2021-12-29T17:06:00.17+00:00

    This is really and Azure SQL question. I suggest you change your tag to "azure-sql-database" for a proper answer.


  2. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2021-12-29T22:32:52.93+00:00

    Yes, you can do that. Demo below.

    CREATE TABLE KeyCardEvents
    (
        EmployeeID INT NOT NULL,
        AccessOperationDescription NVARCHAR (MAX) NOT NULL,
        [Timestamp] Datetime2 NOT NULL,
        StartTransactionId BIGINT GENERATED ALWAYS AS TRANSACTION_ID START HIDDEN NOT NULL,
        StartSequenceNumber BIGINT GENERATED ALWAYS AS SEQUENCE_NUMBER START HIDDEN NOT NULL
    )
    WITH (
        LEDGER = ON (
            LEDGER_VIEW = dbo.[KeyCardEventsLedger] (
                TRANSACTION_ID_COLUMN_NAME = TransactionId,
                SEQUENCE_NUMBER_COLUMN_NAME = SequenceNumber,
                OPERATION_TYPE_COLUMN_NAME = OperationId, 
                OPERATION_TYPE_DESC_COLUMN_NAME = OperationTypeDescription
            ),
            APPEND_ONLY = ON
        )
    );
    go
    EXEC sp_rename 'KeyCardEvents.Timestamp', 'WhenItHappened', 'COLUMN'
    go
    DROP TABLE KeyCardEvents
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.