views:

54

answers:

2

Could you please help me with present SQL?

I am copy 1 table to another table and the same time merge the fields.

I get an error on the +

INSERT INTO [dSCHEMA].[TABLE_COPY_TO]
(
  [FIELD_A],
  [FIELD_B],
  [FIELD_A] + '-' + [FIELD_B]

)
SELECT [FIELD_A]
      ,[FIELD_B]
  FROM [dSCHEMA].[TABLE_COPY_FROM]
+3  A: 

The appended text actually needs to be in the SELECT statement so it would look more like

Insert Into [dSCHEMA].[TABLE_COPY_TO] 
    (FieldA, FieldB, FieldC)
Select FieldA, FieldB, FieldA + '-' + FieldB
From [dSCHEMA].[TABLE_COPY_FROM]
keithwarren7
+1 The real table appears to be called dSCHEMA.TABLE_COPY_TO tho :)
Andomar
A: 

I guess you are trying to create a computed column in [dSCHEMA].[TABLE_COPY_TO].

In such case, you have to define the DDL properly. Below is an example

declare @tblCopyFrom table
(
    fieldA varchar(10)
    ,fieldB varchar(10)
)
declare @tblCopyTo table
(
     fieldA varchar(10)
     ,fieldB varchar(10)
     ,fieldC AS (fieldA + '-' + fieldB) -- Computed Column
)

insert into @tblCopyFrom 
      select 'valA1','valB1' union all
      select 'valA2','valB2' union all
      select 'valA3','valB3' union all
      select 'valA4','valB4' union all
      select 'valA5','valB5' 

insert into @tblCopyTo (fieldA,fieldB)
select * from @tblCopyFrom
select * from @tblCopyTo

Else, before insertion you can add the computed column to your table and then insert like

Alter table TABLE_COPY_TO Add (fieldC AS (fieldA + '-' + fieldB))

insert into.......

priyanka.sarkar