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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:00:19+00:00 2026-06-05T13:00:19+00:00

Initially I have a column ( partner_email ) of type varchar . Now a

  • 0

Initially I have a column (partner_email) of type varchar. Now a recent change has come where it needs to be changed to be changed to the XML datatype but the existing rows needs to be converted into the new column.

Help needed

  • 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-05T13:00:21+00:00Added an answer on June 5, 2026 at 1:00 pm

    Stepping through the scripts will help understand the problem:

    Script 1

    1. Use the Test database
    2. If the column partner_email_temp does not exist in the Partner
      table, add partner_email_temp as an XML column
    3. Update the Partner table. Change the value of partner_email_temp to
      be an XML-friendly string version of partner_email. Then convert it to XML. Use REPLACE to convert all & to the encoded &amp
    4. Drop the column partner_email from Partner
    5. Rename the column partner_email_temp to partner_email

    All of this works the first time. When you run the script a second time, SQL Server gets to Step 2 and executes the ALTER TABLE statement again. This is because the column partner_email_temp does not exist (it was renamed in the first run to partner_email).

    Now in step 3, you are attempting to update the partner_email column again. The problem is that this column is now an XML data type and your use of REPLACE is illegal. So SQL Server throws an error.

    Script 2

    1. Capture the column data type for the partner_email column in
      Partner.
    2. If the column data type is varchar (meaning this is the first pass
      of the script), execute the code in the BEGIN and END
    3. If the column partner_email_temp does not exist, add the
      partner_email_temp column as an XML
    4. Update the Partner table by changing the value of partner_email_temp
      to be an XML-friendly string version of partner_email and then
      convert it to XML. Use REPLACE to convert all & to the encoded &amp
    5. Drop the column partner_email from Partner
    6. Rename the column partner_email_temp to partner_email

    The problem in the second script is that you cannot use the GO statement in your BEGIN and END statement.

    According to BOL:

    SQL Server utilities interpret GO as a signal that they should send
    the current batch of Transact-SQL statements to an instance of SQL
    Server.

    So in script 2, when SQL Server attempts to compile, it notices that the column partner_email_temp doesn’t exist. It works in the Script 1 because the sections are executed separately. And when the portion that references partner_email_temp is executed, the column does exist.

    So how do you fix it?

    You can actually bypass all the dropping and renaming of columns if you simply update the data in the partner_email column to the correct format and then convert the column to an XML data type. This can be achieved in one SQL batch statement, but you need to execute it as dynamic SQL so that SQL Server will only try to execute the REPLACE statement when the partner_email column is a varchar data type:

    IF NOT EXISTS 
    (
        SELECT * 
        FROM INFORMATION_SCHEMA.columns 
        WHERE table_name = 'Partner'
        AND   column_name = 'partner_email'
        AND   data_type = 'xml'
    )
    BEGIN
    
      DECLARE @sqlCmd NVARCHAR(4000)
    
      SET @sqlCmd = N' 
      UPDATE [dbo].[Partner]
      SET partner_email = ''<PartnerEmails><Email>'' 
      + REPLACE(partner_email, ''&'', ''&amp;'') 
      + ''</Email><Email></Email><Email></Email></PartnerEmails>'';
      ALTER TABLE Partner ALTER Column partner_email XML
      '
    
      EXEC (@sqlCmd) 
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Customer class which has a representative field....this field is initially blank
I have a page that has 4 tables. Initially when the page is loaded,
Suppose I have a table with a column name varchar(20) , and I store
I have a table with 1 column of varchar values. I am looking for
I have a table with a status column (initially with progress ). I need
I have a table in which in one column some content is initially hidden,
I have an iframe on a page that I need to initially have a
I have this jQuery datepicker code that initially set the minDate of the datepicker.
I have a page with a list of items initially loaded from a include_once('show_calendarV2.php');
I have a page where I am loading bunch of images initially when the

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.