Home > Software design >  Conversion failed when converting varchar to data type int... but field is varchar [closed]
Conversion failed when converting varchar to data type int... but field is varchar [closed]

Time:09-25

I get an error on the 2nd line running the below code

Conversion failed when converting the varchar value 'apple' to data type int

create table ##temp (FieldA numeric(20,10), FieldB numeric(20,10), FieldC varchar(255))

insert into ##temp values(1.1, 2.1, 3.1), (4.1, 5.1, 6.1), ('apple', 'pear', 'orange')

Why does it expect an int when varchar is specified when making the table?

CodePudding user response:

You are confusing rows and columns. You want:

insert into ##temp (FieldA, FieldB, FieldC)
    values (1.1, 4.1, 'apple'),
           (2.1, 5.1, 'pear'),
           (3.1, 6.1, 'orange');

Each parenthesized expression for values is a row not the values in one column.

Also note that I included the column names for the insert. This is a best practice.

  • Related