pratik watwani
pratik watwani

Reputation: 137

Insert into table errors

Code:

create table student
( 
    s_id int not null primary key identity(1,1),
    s_fname varchar(30) not null,
    s_lname varchar(50) not null,
    s_branch varchar(4) not null,
    pointer float(50) ,
    s_dob date not null
);

drop table student;

select * 
from student;

insert into student (s_fname, s_lname, s_branch, pointer, s_dob)
values (('abc', 'xyz', 'CMPN', 8.5, '1996-03-14'),
        ('lmn', 'pqr', 'EXTC', 7, '1997-03-23')
       );

I am unable to rectify what is the error.

Error message I get is

Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ','

Upvotes: 0

Views: 53

Answers (3)

DhruvJoshi
DhruvJoshi

Reputation: 17126

So I believe you had a drop table statement to ensure that the table should not exist before you start creation of the table.

the correct way of doing this is like below where we drop the existing student table only if it exists, and then create it , populate it and then run a select * on it

if object_id('dbo.student', 'u') is not null 
  drop table dbo.student; 

create table student
( 
s_id int not null primary key identity(1,1),
s_fname varchar(30) not null,
s_lname varchar(50) not null,
s_branch varchar(4) not null,
pointer float(50) ,
s_dob date not null
);

insert into student (s_fname,s_lname,s_branch,pointer,s_dob)
values
('abc','xyz','CMPN',8.5,'1996-03-14'),
('lmn','pqr','EXTC',7,'1997-03-23');

select * from student;

Upvotes: 0

Walter_Ritzel
Walter_Ritzel

Reputation: 1397

According to my comment above, I think your code should be like this:

create table student
( 
  s_id int not null primary key identity(1,1),
  s_fname varchar(30) not null,
  s_lname varchar(50) not null,
  s_branch varchar(4) not null,
  pointer float(50) ,
  s_dob date not null
);

--drop table student;


insert into student (s_fname,s_lname,s_branch,pointer,s_dob)
values ('abc','xyz','CMPN',8.5,'1996-03-14'),
('lmn','pqr','EXTC',7,'1997-03-23');

select * from student;

Upvotes: 0

Lukasz Szozda
Lukasz Szozda

Reputation: 175566

You need to remove outermost () in VALUES clause:

create table student
( 
  s_id int not null primary key identity(1,1),
  s_fname varchar(30) not null,
  s_lname varchar(50) not null,
  s_branch varchar(4) not null,
  pointer float(50) ,
  s_dob date not null
);

insert into student (s_fname,s_lname,s_branch,pointer,s_dob)
values
('abc','xyz','CMPN',8.5,'1996-03-14'),
('lmn','pqr','EXTC',7,'1997-03-23');

select * from student;

LiveDemo

And drop table student; should be removed/commented too.

Upvotes: 3

Related Questions