Avatar of Nemetona
Nemetona
 asked on

copying a record with a timestamp field

I wish to insert records from one table into another in the same database.

Both tables have the same structure.

I am using

use mydatabase
insert into Table2
select * from Table1

but I receive the error message "Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column."

I then tried to use the column list method
USE [mydatabase]
GO

INSERT INTO [dbo].[Table2]
           ([Field1]
           ,[Field2]
           ,[Field3]
           )
     VALUES
           (dbo.Table1.Field1
           ,dbo.Table1.Field2
           ,dbo.Table1.Field3
           )
GO

but this resulted in a list of error messages such as:
"The multi-part identifier "dbo.Table2.Field1" could not be bound."


Can anyone help me update this table?
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Nemetona

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Dan Craciun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lawrence Barnes

it also could be that the timestamp column is locked down and the DBA has it defaulting to the current time.

First try this:

Change to:
INSERT INTO [dbo].[Table2]
           ([Field1]
           ,[Field2]
           ,[Field3]
           )
 SELECT Field1, Field2, Field3
FROM dbo.Table1
GO

Otherwise remove the field that has the timestamp and try again.

LVBarnes
Scott Pletcher

INSERT INTO [dbo].[Table2]
           ([Field1]
           ,[Field2]
           ,[Field3]
           )
SELECT
           Field1
           ,Field2
           ,DEFAULT AS Timestamp_Column
FROM dbo.Table1
GO
Nemetona

ASKER
Many thanks, that worked well.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck