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

  • Home
  • SEARCH
  • 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 59797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:03:42+00:00 2026-05-10T18:03:42+00:00

I want to find a sql command or something that can do this where

  • 0

I want to find a sql command or something that can do this where I have a table named tblFoo and I want to name it tblFooBar. However, I want the primary key to also be change, for example, currently it is:

CONSTRAINT [PK_tblFoo] PRIMARY KEY CLUSTERED  

And I want a name change to change it to:

CONSTRAINT [PK_tblFooBar] PRIMARY KEY CLUSTERED  

Then, recursively go through and cascade this change on all tables that have a foreigh key relationship, eg. from this:

CHECK ADD  CONSTRAINT [FK_tblContent_tblFoo] FOREIGN KEY([fooID]) 

To this:

 CHECK ADD  CONSTRAINT [FK_tblContent_tblFooBar] FOREIGN KEY([fooID]) 

Naturally, I am trying not to go through and do this all manually because a) it is an error prone process, and b)it doesn’t scale.

  • 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. 2026-05-10T18:03:42+00:00Added an answer on May 10, 2026 at 6:03 pm

    This is just off the top of my head and isn’t complete (you’d need to add similar code for indexes). Also, you would need to either add code to avoid renaming objects from a table with the same base name, but additional characters – for example, this code would also list tblFoo2 and all of its associated objects. Hopefully it’s a start for you though.

    DECLARE     @old_name   VARCHAR(100),     @new_name   VARCHAR(100)  SET @old_name = 'tblFoo' SET @new_name = 'tblFooBar'  SELECT     'EXEC sp_rename ''' + name + ''', ''' + REPLACE(name, @old_name, @new_name) + '''' FROM dbo.sysobjects WHERE name LIKE '%' + @old_name + '%' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Thats not garbage. It is just HTML sanitized string of… May 12, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer 32bit operating systems CAN see more than 4GB of memory… May 12, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer Ok, I finally found a solution. Gustavo you were on… May 12, 2026 at 8:32 pm

Related Questions

Good morning, all. This is my first question on stackoverflow, so hopefully this isn't
There is a need from a customer to log every data change to a
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I am using Angel LMS and its built on a SQL Server platform. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.