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.
| Author |
Topic |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-03-13 : 01:45:30
|
| I have this as a test:Create Table #t([FirstName] nvarchar(50), [MiddleName] nvarchar(50), [LastName] nvarchar(50), [Initials] nvarchar(50), [LoginName] nvarchar(50), [LoginPassword] nvarchar(50), [IsDeleted] bit, [ProfileXMLFile] varbinary(MAX), [UserImageFileName] nvarchar(MAX), [UserNotes] nvarchar(MAX), [LastLoginTime] DateTime)Insert into #tSelect [FirstName], [MiddleName], [LastName], [Initials], [LoginName], [LoginPassword], [IsDeleted], [ProfileXMLFile], [UserImageFileName], [UserNotes], [LastLoginTime]From UserDetailsSelect * From #t****Works upto here*********In the database the field before [FirstName] is [UserID] this is an auto generated key. How do I do this below to insert into my UserDetails table a unique key followed by all my fields?*********SET IDENTITY_INSERT dbo.UserDetails ONInsert into UserDetailsSelect [FirstName], [MiddleName], [LastName], [Initials], [LoginName], [LoginPassword], [IsDeleted], [ProfileXMLFile], [UserImageFileName], [UserNotes], [LastLoginTime]From #tDrop Table #tSelect * From UserDetailsParses but I get this error when run:Msg 545, Level 16, State 1, Procedure Import_from_Excel_to_DB, Line 42Explicit value must be specified for identity column in table 'UserDetails' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-13 : 02:32:50
|
| did u kept identity function for userid column or not? check it once |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-03-13 : 02:45:05
|
| Sorry bklr I do not understand what you mean? The table UserDetails has a key called UserID. This is set to Identity (1,1). |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-13 : 02:58:09
|
| why r u using set identity_insert tablename onwith out that u can insert the values in to userdetails |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-03-13 : 03:06:14
|
| Yes, I tried without SET IDENTITY_INSERT dbo.UserDetails ON but it doesnt work.Create Table #t([FirstName] nvarchar(50), [MiddleName] nvarchar(50), [LastName] nvarchar(50), [Initials] nvarchar(50), [LoginName] nvarchar(50), [LoginPassword] nvarchar(50), [IsDeleted] bit, [ProfileXMLFile] varbinary(MAX), [UserImageFileName] nvarchar(MAX), [UserNotes] nvarchar(MAX), [LastLoginTime] DateTime)Insert into #tSelect [FirstName], [MiddleName], [LastName], [Initials], [LoginName], [LoginPassword], [IsDeleted], [ProfileXMLFile], [UserImageFileName], [UserNotes], [LastLoginTime]From UserDetailsSelect * From #t****Works upto here but note exclude UserID from UserDetails as this is the auto generated number*********Below I just need to insert the fields from my temp table back into UserDetails so at the end of the day the UserDetails should have the data for all field duplicated but the second lot fromthe temp table with new UserID keys****Insert into UserDetailsSelect [FirstName], [MiddleName], [LastName], [Initials], [LoginName], [LoginPassword], [IsDeleted], [ProfileXMLFile], [UserImageFileName], [UserNotes], [LastLoginTime]From #tDrop Table #tSelect * From UserDetails |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-13 : 03:18:35
|
| did u get any error with out identity_insert onif u get any error post it once |
 |
|
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-03-13 : 22:04:12
|
| This is the error I get:(3 row(s) affected)(3 row(s) affected)Msg 2601, Level 14, State 1, Procedure Import_from_Excel_to_DB, Line 42Cannot insert duplicate key row in object 'dbo.UserDetails' with unique index 'IX_UserDetails'.The statement has been terminated.(3 row(s) affected)(1 row(s) affected)Result set 1:FirstName MiddleName LastName Initials LoginName IsDeleted Profile UserImageName UserNotes LastLogin NULL NULL tester Xtralis_$777888 0 NULL NULL NULL NULL NULL NULL Admin password 0 NULL NULL NULL NULL NULL NULL test test 0 NULL NULL NULL NULLResult set 2:UserID (key) FirstName MiddleName LastName Initials LoginName IsDeleted Profile UserImageName UserNotes LastLogin1 NULL NULL tester Xtralis_$777888 0 NULL NULL NULL NULL2 NULL NULL Admin password 0 NULL NULL NULL NULL3 NULL NULL test test 0 NULL NULL NULL NULLUserID Settings:Identity Specification YesIs Identity YesIndentity Increment 1Identitiy Seed 1 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-03-14 : 03:01:44
|
| do u have any unique constraint on any column in ur table? |
 |
|
|
|
|
|
|
|