2

I am am using sql server 2005 and doing a simple insert into and getting an incorrect syntax error. I See nothing wrong with my code Can someone give me some ideas what could be wrong with it?

insert into inonhd 
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
  ('CRV109','1','11','01','1'), 
  ('CRV110','0','11','01','0')

the error is Incorrect syntax near ','.

Turtleman10
  • 119
  • 1
  • 2
  • 14
  • 1
    Which version of SQL Server are you using? What are the datatypes for the columns? – Joseph B May 20 '14 at 16:15
  • 2
    Read [This Thread](http://stackoverflow.com/questions/2462517/insert-multiple-values-using-insert-into-sql-server-2005). Maybe it helps. – Jens May 20 '14 at 16:16
  • 1
    Thanks that thread answered my question. I am using 2005 so I have to do it one by one. – Turtleman10 May 20 '14 at 16:34

2 Answers2

4

You must add each row in separate command.

insert into inonhd 
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV109','1','11','01','1')

and:

insert into inonhd 
(fpartno,fpartrev,flocation,fonhand,fcudrev)
Values
('CRV110','0','11','01','0')
Majid
  • 13,853
  • 15
  • 77
  • 113
Ahmad
  • 906
  • 11
  • 27
  • 1
    SQL 2008 and later supports [multiple inserts in one statement](http://technet.microsoft.com/en-us/library/dd776382.aspx) – D Stanley May 20 '14 at 16:24
0

It is really important to note that the syntax in the question is fine for more recent versions of SQL Server. This is acceptable:

insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
    Values ('CRV109','1','11','01','1'), 
           ('CRV110','0','11','01','0');

If you want to do this in one statement, you can use select . . . union all:

insert into inonhd(fpartno, fpartrev, flocation, fonhand, fcudrev)
    select 'CRV109','1','11','01','1' union all
    select 'CRV110','0','11','01','0';

Of course, multiple inserts are another possibility.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786