1

I have two SQL Server tables, and I want to insert data from one into another.

I only want two columns from table A to be inserted into table B (which has more than two columns). I want to add a bunch of nulls and 1s into the rest of table B.

I saw this answer which helped, but what if I want to, for example:

INSERT into B(name, address, foo, bar, blah)
select name, 'B'+address from table A, 1, 1, null

'B'+address is throwing the error
Conversion failed when converting the varchar value 'B' to data type int

Community
  • 1
  • 1
Amanda_Panda
  • 1,156
  • 4
  • 26
  • 68

1 Answers1

0

Try this:

 INSERT INTO B (name, address, foo, bar, blah)
 SELECT name, 'B'+ cast(address as varchar(250)), 1, 1, NULL
 FROM table A

'or

; with CTE 
 as

 (
 SELECT NAME, 'B'+ cast(address as varchar(250)) [address], 1 AS FOO, 1 AS BAR, NULL AS BLAH 
 FROM TABLE A

 )

 INSERT INTO B (NAME,[ADRESS], FOO, BAR, BLAH)
 SELECT NAME, [ADRESS], FOO, BAR, BLAH 
 FROM CTE
Jande
  • 1,695
  • 2
  • 20
  • 32