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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:30:47+00:00 2026-06-14T10:30:47+00:00

I have a table with the primary key event_id . I need to change

  • 0

I have a table with the primary key event_id. I need to change the Plant from IM17 to SG18.

I do not want to delete the rows (being kept for historical reasons). I get a PK violation when executing the following SQL.

DECLARE @plant CHAR(4)
DECLARE @l_event_id INT

SELECT @plant = 'SG18'
SET @l_event_id = (SELECT MAX(cast(event_id as int)) FROM dbo.event_header) 

INSERT INTO dbo.event_header (
            event_id, scenario_name, actor_code, method_code,
            SAP_plant_code, object_id,serial_no
            )
    SELECT @l_event_id + 1 , eh.scenario_name, eh.actor_code,
           eh.method_code, @plant, eh.object_id, eh.serial_no
    FROM dbo.event_header eh
    WHERE eh.SAP_plant_code = 'IM17';
  • 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-14T10:30:48+00:00Added an answer on June 14, 2026 at 10:30 am

    Your approach doesn’t work, since you evaluate the MAX(cast(event_id as int)) only once – and then try to insert all n new rows with the same value for event_id….

    You’ll need to use something like this to get your job done:

    DECLARE @plant CHAR(4) = 'SG18'
    
    DECLARE @l_event_id INT
    SELECT @l_event_id = MAX(CAST(event_id AS INT)) FROM dbo.event_header) 
    
    ;WITH CTE AS
    (
        SELECT 
            eh.scenario_name, 
            eh.actor_code,
            eh.method_code, 
            eh.object_id, 
            eh.serial_no,
            RowNum = ROW_NUMBER() OVER (ORDER BY eh.serial_no)
        FROM dbo.event_header eh
        WHERE eh.SAP_plant_code = 'IM17';
    )
    INSERT INTO 
        dbo.event_header (event_id, scenario_name, actor_code, method_code, SAP_plant_code, object_id,serial_no)
        SELECT
            @l_event_id + RowNum,
            scenario_name, actor_code, method_code, 
            @plant, 
                object_id, serial_no
        FROM
            CTE  
    

    Basically, this CTE (Common Table Expression) gets all the values you need, plus it uses ROW_NUMBER() to generate sequential numbers (from 1 to the number of rows selected for @plant = 'IM17').

    When you add that RowNum to the previous max value, and nothing else is inserted data into that target table right now – then you stand a chance of success!

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

Sidebar

Related Questions

I have a table with primary key (ColumnA, ColumnB). I want to make a
I have a table with a primary key that is auto increment. I want
I have table A in Oracle that has a primary key (id). I need
I have table with 4 primary key fields. I load that in to drop
I have a table containing primary key and foreign key that references same table.
I have a table whose primary key is a column named St_ID . I
I have a table with composite primary key. I am trying to load data
I have table with a unique auto-incremental primary key. Over time, entries may be
In my database I have table AnimalDetails containing animalNumber(primary key),name,breed dateofbirth and an image
I have a table that has no primary key, and one cannot be created

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.