Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8112451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:30:22+00:00 2026-06-06T02:30:22+00:00

I have a ReferenceID varchar(6) column in over 80 different tables. I need to

  • 0

I have a ReferenceID varchar(6) column in over 80 different tables. I need to extend this to a varchar(8) throughout the db following a change implemented by the government organisation that assigns the IDs.

I was hoping to declare a cursor to get the table names as follows:

DECLARE @TableName AS VARCHAR(200)
DECLARE TableCursor CURSOR LOCAL READ_ONLY FOR
SELECT t.name AS TableName
    FROM sys.columns c
        JOIN sys.tables t ON c.object_id = t.object_id
    WHERE c.name = 'ReferenceID'

OPEN TableCursor
    FETCH NEXT FROM TableCursor 
    INTO @TableName

and then edit the type as follows:

ALTER TABLE @TableName ALTER COLUMN ReferenceID VARCHAR(8)

This fails because the column is part of the Primary Key in some of the tables (and the columns included in the PK vary from table to table).

I really don’t want to have to drop and recreate each PK manually for each table.

Within the cursor, is there a way either to disable the PK before altering the datatype and then re-enable it, or to drop and recreate the PK either side of altering the datatype, bearing in mind that the PK will depend on which table we’re currently looking at?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:30:23+00:00Added an answer on June 6, 2026 at 2:30 am

    You need to specify NOT NULL explicitly in an ALTER TABLE ... ALTER COLUMN otherwise it defaults to allowing NULL. This is not permitted in a PK column.

    The following works fine.

    CREATE TABLE p
    (
    ReferenceID VARCHAR(6) NOT NULL PRIMARY KEY
    )
    
    INSERT INTO p VALUES ('AAAAAA')
    
    ALTER TABLE p ALTER COLUMN ReferenceID VARCHAR(8) NOT NULL
    

    when the NOT NULL is omitted it gives the following error

    Msg 5074, Level 16, State 1, Line 1
    The object 'PK__p__E1A99A792180FB33' is dependent on column 'ReferenceID'.
    Msg 4922, Level 16, State 9, Line 1
    ALTER TABLE ALTER COLUMN ReferenceID failed because one or more objects access this column.
    

    A couple of things to consider in your programmatic approach is that you would need to drop any foreign keys referencing the ReferenceID columns temporarily and also make sure you don’t include the NOT NULL for (Non PK) ReferenceID columns that currently are nullable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: ID1 INTEGER PRI ID2 INTEGER PRI NAME VARCHAR now I need
I have a set of tables in MySQL like this (foreign keys referenced by
I have a table with separate columns for months, days and a varchar column
I have tables foo and bar: create table foo(a int, b varchar(10), primary key
I have this sp. I just found out that M.MDS_FILE column, contains a file
I have two tables: invoices & invoice_items. I need a one to many relationship
I have a table that has two foreign keys to two different tables with
I am wanting to have a label column ( VARCHAR ) and I want
Okay so I have two tables: Companies | id int | name varchar(50) and
I have a table named KEYWORDS with a column named ENTRY of VARCHAR(10). Would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.