Luk
Luk

Reputation: 15

Copy data from one table to another while at the same time insert a guid SQL

I have over 13,000 records in an existing table called "AnyName"

I need to move the records to a new table Called "DomainObject2" New, this table will have a Guid primary column named "Oid"

I get the following error when i execute the below code.

Cannot insert the value NULL into column 'Oid', table 'NewDatabase.dbo.DomainObject2'; column does not allow nulls. INSERT fails.

    USE NewDatabase

INSERT INTO NewDatabase.dbo.DomainObject2

 ([Ano]
           ,[C1]
           ,[C2]
           ,[C3]
           ,[C4]
           ,[C5]
           ,[CF]
           ,[CF2]
           ,[CF3]
           ,[CF4]
           ,[CF5])
SELECT 
[ano]
           ,[c1]
           ,[c2]
           ,[c3]
           ,[c4]
           ,[c5]
           ,[cf]
           ,[cf2]
           ,[cf3]
           ,[cf4]
           ,[cf5]
FROM AnyName.dbo.[Old_Table]; 

GO```



Thanks in advance!!!

Upvotes: 0

Views: 411

Answers (1)

rumata28
rumata28

Reputation: 395

Try this:

INSERT INTO NewDatabase.dbo.DomainObject2

 ([Oid]
           ,[Ano]
           ,[C1]
           ,[C2]
           ,[C3]
           ,[C4]
           ,[C5]
           ,[CF]
           ,[CF2]
           ,[CF3]
           ,[CF4]
           ,[CF5])
SELECT     NewID()
           ,[ano]
           ,[c1]
           ,[c2]
           ,[c3]
           ,[c4]
           ,[c5]
           ,[cf]
           ,[cf2]
           ,[cf3]
           ,[cf4]
           ,[cf5]
FROM AnyName.dbo.[Old_Table]; 

Upvotes: 2

Related Questions