none
How to copy exist record on table to new row with same data but different ID ? RRS feed

  • Question

  • Problem

    How to copy exist record on table  to new row with same data but different ID ?

    I have table name Employee as following 

    CREATE TABLE [dbo].[Employees](
    	[EmployeeId] [int] NOT NULL,
    	[EmployeeName] [nvarchar](max) NULL,
    	[EmployeeAge] [int] NOT NULL,
    	[BirthDate] [datetime2](7) NOT NULL,
    	
     CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
    (
    	[EmployeeId] ASC
    
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    
    GO

    and data Sample

    EmployeeId EmployeeName	 EmployeeAge	  BirthDate	
    1	        micheel	        33	  1985-01-10 00:00:00.0000000	
    2	        elias	        28	  1990-02-25 00:00:00.0000000	
    4	        adel	        22	  1995-12-12 00:00:00.0000000	
    5	        gorg	        38	  1980-12-01 00:00:00.0000000	

    Actually I need to copy data of EmployeeId number 4 to New record Number 6

    and different name as mariam so that record number 6 will be as following

    6	        mariam	        22	1995-12-12 00:00:00.0000000

    I work on sql server 2012 visual studio 2015 and i use c# language and i using ado.net technique 

    so that how to copy exist row to new row but change only will be in two fields 

    EmployeeId and EmployeeName

    thanks


    Tuesday, March 19, 2019 2:14 AM

Answers

  • in my case Employeeid not identity also

    i have some table have 60  column so that if i make insert statment

    to 60 column this is more effort 

    i need only take different on two column and another

    columns is similar on two records 

    i need to make any thing copy 58 columns from one record to another

    and different only i assign to it

    You can have a GUID as the  primary-key column for the new record. You can put the ID from the record you are copying from into the new record so that you could have a reference  back to the original record ID, if needed. 

    https://www.sqlshack.com/understanding-the-guid-data-type-in-sql-server/

    Tuesday, March 19, 2019 9:53 AM

All replies

  • Hi engahmedbarbary,

    Thank you for posting here.

    For your question, you could make the ID increased automatically.

    [EmployeeId] [int] IDENTITY (1, 1) NOT NULL,

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 19, 2019 6:00 AM
    Moderator
  • in my case Employeeid not identity also

    i have some table have 60  column so that if i make insert statment

    to 60 column this is more effort 

    i need only take different on two column and another

    columns is similar on two records 

    i need to make any thing copy 58 columns from one record to another

    and different only i assign to it

    Tuesday, March 19, 2019 7:52 AM
  • in my case Employeeid not identity also

    i have some table have 60  column so that if i make insert statment

    to 60 column this is more effort 

    i need only take different on two column and another

    columns is similar on two records 

    i need to make any thing copy 58 columns from one record to another

    and different only i assign to it

    You can have a GUID as the  primary-key column for the new record. You can put the ID from the record you are copying from into the new record so that you could have a reference  back to the original record ID, if needed. 

    https://www.sqlshack.com/understanding-the-guid-data-type-in-sql-server/

    Tuesday, March 19, 2019 9:53 AM