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 8687033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:01:00+00:00 2026-06-12T23:01:00+00:00

I have a table that is currently using a couple of columns named DateFrom

  • 0

I have a table that is currently using a couple of columns named DateFrom and DateTo. I’m trying to replace them with a single NewDate column, populated for existing rows with the value from DateFrom.

I need good error/transaction handling as, if the change fails, I don’t want a halfway in-between table, I want to revert.

I’ve tried a number of things but can’t get it to work properly. Any help is appreciated as I’m far from experienced with this.

I started with

BEGIN TRAN

ALTER TABLE TableName
ADD NewDate DATETIME

IF @@ERROR = 0 AND @@TRANCOUNT = 1
UPDATE TableName
SET NewDate = ValidFrom
....

This fails immediately as NewDate is not currently a column in the table. Fine, so I add a GO in there. This breaks it into two batches and it now runs, except it makes the @@ERROR check pointless. I also can’t use a local variable as those are lost after GO as well. Ideally I’d like to use a TRY...CATCH to avoid checking errors after each statement but I can’t use a GO with that as it needs to be one batch.

None of the articles I’ve found talk about this situation (error handling with GO). So the question is: Is there any way I can get the transaction-with-error-handling approach I’m looking for when adding and updating a column (which seems to necessitate a GO somewhere)?

Or am I going to have to settle for doing it in several batches, without the ability to roll back to my original table if anything goes wrong?

  • 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-12T23:01:02+00:00Added an answer on June 12, 2026 at 11:01 pm

    Why are you worried about creating the new column in the transaction? Just create the column and then populate it. You don’t even need an explicit tran when populating it. If it fails (which is very unlikely), just do the update again.

    I would do the following steps

    1. Add new column
    2. Update new column
    3. Check to see if the data in the new column looks correct
    4. Drop the old columns no longer needed (you may want to check where these columns are being used before dropping them e.g. are they used in any stored procedures, reports, front-end application code)

    Also, it is worth adding more context to your question. I assume you are testing a script against a test database and will later apply the script to a prod database. Is the prod database very big? Very busy? Mission critical? Backed up on a schedule?

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

Sidebar

Related Questions

I'm trying to normalize a mysql database.... I currently have a table that contains
Greetings, I have some mysql tables that are currently using an md5 hash as
I have a table that has an AUTO_INCREMENT field. Currently, it is also a
I currently have a table structure that looks something like this(some details omitted): ColumnName
Currently I have a User table that has a toys_owned column that is an
So currently I have a table in Lua that contains another table (much like
I currently have a table with an id column that is being autoincremented and
Currently I have a powershell proccess that is scanning a SQL Server Table and
I have implemented a UISearchDisplayController that allows users to search a table. Currently the
I have a single table that shows employee deployments, for various types of deployment,

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.