Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 new sql need help

Author  Topic 

cadnaani
Starting Member

6 Posts

Posted - 2008-10-05 : 04:15:36
I created this table and I am getting this syntax error please help!
Cannot insert explicit value for identity column in table 'fudgemart_timesheets' when IDENTITY_INSERT is set to OFF.

create table fudgemart_timesheets
(
timesheet_id int identity (1,1)not null ,
timesheet_payrolldate datetime not null,
timesheet_employee_id int not null,
timesheet_hours decimal(3,1)
);

--add foriegn key constriants
alter table fudgemart_timesheets
add
constraint pk_timesheet_id
primary key (timesheet_id),
constraint fk_timesheet_employee_id
foreign key (timesheet_employee_id)
references fudgemart_employees(employee_id),
constraint ck_timesheet_hours check (timesheet_hours between 0 and 60)


--Add six employees in the timesheets table
insert into fudgemart_timesheets
(
timesheet_id,
timesheet_employee_id,
timesheet_payrolldate,
timesheet_hours
)
values
(
1,
'010101007',
'9/10/2007',
'36'
)

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-05 : 04:41:14
[code]SET IDENTITY_INSERT fudgemart_timesheets ON

insert into fudgemart_timesheets
(
timesheet_id,
timesheet_employee_id,
timesheet_payrolldate,
timesheet_hours
)
values
(
1,
'010101007',
'9/10/2007',
'36'
)

SET IDENTITY_INSERT fudgemart_timesheets OFF[/code]

question is why do you want to save an explicit value for timesheet_id after having defined it as identity column?
Go to Top of Page

cadnaani
Starting Member

6 Posts

Posted - 2008-10-05 : 10:25:47
quote:
Originally posted by cadnaani

I created this table and I am getting this syntax error please help!
Cannot insert explicit value for identity column in table 'fudgemart_timesheets' when IDENTITY_INSERT is set to OFF.

create table fudgemart_timesheets
(
timesheet_id int identity (1,1)not null ,
timesheet_payrolldate datetime not null,
timesheet_employee_id int not null,
timesheet_hours decimal(3,1)
);

--add foriegn key constriants
alter table fudgemart_timesheets
add
constraint pk_timesheet_id
primary key (timesheet_id),
constraint fk_timesheet_employee_id
foreign key (timesheet_employee_id)
references fudgemart_employees(employee_id),
constraint ck_timesheet_hours check (timesheet_hours between 0 and 60)


--Add six employees in the timesheets table

insert into fudgemart_timesheets
(
timesheet_id,
timesheet_employee_id,
timesheet_payrolldate,
timesheet_hours
)
values
(
1,
'010101007',
'9/10/2007',
'36'
)


Thanks, I did what you told me but it did not work. Now I am getting this syntax error.
The INSERT statement conflicted with the FOREIGN KEY constraint "fk_timesheet_employee_id". The conflict occurred in database table "dbo.fudgemart_employees", column 'employee_id'.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-05 : 13:24:39
thats because the value you're trying to insert for timesheet_employee_id does not exist in employee_id column of parent table dbo.fudgemart_employees to which its related by foreign key. so if you want to insert this value, you need to first insert an record to fudgemart_employees table for this employee_id.
Go to Top of Page
   

- Advertisement -