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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:27:26+00:00 2026-05-25T15:27:26+00:00

Here is some Transact-SQL I am trying to run via sqlcmd (SQL Server 2005).

  • 0

Here is some Transact-SQL I am trying to run via sqlcmd (SQL Server 2005).

USE PUK;
GO
BEGIN TRANSACTION;
BEGIN TRY

   -- - Modify RETRIEVAL_STAT
   alter table dbo.RETRIEVAL_STAT add
      SOURCE nvarchar(10) NULL,
      ACCOUNTNUMBER nvarchar(50) NULL,
      PUK nvarchar(20) NULL;

   -- transform logic.
   update dbo.RETRIEVAL_STAT set
      SOURCE = 'XX',
      ACCOUNTNUMBER = 'XX',
      PUK = 'XX';

END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;
    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;
IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
GO

I am getting the following error:

(0 rows affected)
Changed database context to 'PUK'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'SOURCE'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'ACCOUNTNUMBER'.
Msg 207, Level 16, State 1, Server localhost\SQLEXPRESS, Line 11
Invalid column name 'PUK'.

I am guessing that this is because the new columns introduced by the alter statement have not yet been committed, so that the update fails.

My question is how do I get this to work then? I want this to run as a single transaction that I can rollback if something goes wrong.. This is important because I have more alter statements to include yet, and am a bit frustrated that I can’t get past this point.

Any assistance would be most appreciated!

Rob
🙂

  • 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-05-25T15:27:26+00:00Added an answer on May 25, 2026 at 3:27 pm

    Even though I am writing my own answer – all credit goes to @Mikael Eriksson, who suggested that I need to separate different batches with a GO – so that the code that alters the table does not conflict with code that uses the altered table. Thanks Mikael!

    USE PUK;
    GO
    BEGIN TRANSACTION;
    BEGIN TRY
    
       -- - Modify RETRIEVAL_STAT
       alter table dbo.RETRIEVAL_STAT add
          SOURCE nvarchar(10) NULL,
          ACCOUNTNUMBER nvarchar(50) NULL,
          PUK nvarchar(20) NULL;
    
    END TRY
    BEGIN CATCH
        SELECT
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
    END CATCH;
    IF @@TRANCOUNT > 0
        COMMIT TRANSACTION;
    GO
    
    
    USE PUK;
    GO
    BEGIN TRANSACTION;
    BEGIN TRY
    
        -- transform logic.
       UPDATE      dbo.RETRIEVAL_STAT
       SET         SOURCE = 'ABC',
                   ACCOUNTNUMBER = ABC.ACCOUNTNUMBER,
                   PUK = ABC.PUK
       FROM        RETRIEVAL_STAT RS
       INNER JOIN  ABC
       ON          RS.SERVICE_NUMBER = ABC.SERVICENUMBER;
    
       UPDATE      dbo.RETRIEVAL_STAT
       SET         SOURCE = 'DEF',
                   ACCOUNTNUMBER = DEF.BILLINGACCOUNTNUMBER ,
                   PUK = DEF.PUK
       FROM        RETRIEVAL_STAT RS
       INNER JOIN  DEF
       ON          RS.SERVICE_NUMBER = DEF.SERVICENUMBER;
    
       UPDATE      dbo.RETRIEVAL_STAT
       SET         SOURCE = 'No Match'
       WHERE       SOURCE IS NULL;
    
        -- Fix other columns that should be not nullable.
       alter table dbo.RETRIEVAL_STAT
          alter column SERVICE_NUMBER nvarchar (50) NOT NULL;
       alter table dbo.DEF
          alter column PUK nvarchar (20) NOT NULL;
    
    
    END TRY
    BEGIN CATCH
        SELECT
            ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
    END CATCH;
    IF @@TRANCOUNT > 0
        COMMIT TRANSACTION;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's some background on what I'm trying to do: Open a serial port from
I tried to add an index on a view in Sql Server 2005 an
I read here some reasons to use constraints instead of triggers. But I have
i want to use doctrine with zend_paginator here some example query : $allArticleObj =
Here is some code I could not get to format properly in markdown, this
Here is some simple Perl to count the number of times a value occurs
Here's some Ruby code: puts %x{ pstree #{$$} } # never forks puts %x{
Here's some code I saw once. Can you see what's wrong with it? [updated]
Here is some XAML <HierarchicalDataTemplate DataType={x:Type data:FolderEntity} ItemsSource={Binding Path=FolderEntities,UpdateSourceTrigger=PropertyChanged}> <Label Content={Binding FolderName}/> </HierarchicalDataTemplate> <TreeView/>
here is some sample javascript: SomeObjectType = function() { } SomeObjectType.prototype = { field1:

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.