Home > Back-end >  Copying Data from 1 table to another , the second table has 2 extra columns with data that must be i
Copying Data from 1 table to another , the second table has 2 extra columns with data that must be i

Time:02-08

Copying data from 1 table to another, the second table has 2 extra columns with data that must be inserted. I know I must use an insert into statement and possibly make it nested ,also maybe use @ for variables or something my knowledge is limited and I need to do this in a single query. There can't be nulls

My current code that isn't working:

INSERT INTO SampleExtraTable_2 (SampleID, SampleName, SampleEmail)
SELECT *
       (
       SELECT Extra1,Extra2
       FROM SampleExtraTable_2  
       Values('b','r');
       )
FROM SampleTable_1
WHERE 1 = 1

SampleExtraTable_2 has the following fields all defined as char(10), (SampleID, SampleName, SampleEmail,Extra1,Extra2)

SampleTable_1 two has the same fields except the extras and definition amounts are all char(10)

Pic 1

pc 2 extra

Results example

CodePudding user response:

Youre trying to insert NULL values.

   SELECT Extra1,Extra2
   FROM SampleExtraTable_2  

Your inner select query is returning NULL.

You can insert 'dta', 'dta2' values as a string to your new table like below. (if they are constant)

INSERT INTO SampleExtraTable_2 (SampleID, SampleName, SampleEmail, Extra1, Extra2)
SELECT SampleID, SampleName, SampleEmail, 'dta', 'dta2'
FROM SampleTable_1
  •  Tags:  
  • Related