Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello everyone,

I am trying to insert new records from table (UserMgmt) to another table (UserSignInHistory), the fields in these two tables are same. i want each record to be inserted once per day. This is my code:

SQL
INSERT INTO UserSignInHistory 
		SELECT [Staffid],[Username],[Surname],[FirstName],[Branch],[UserRoles],[DateCreated],[SignInDate],[IsActive],GETDATE()
		FROM [UserMgmt] um
		WHERE
        NOT EXISTS (SELECT * FROM UserSignInHistory ss
		
		WHERE ((um.SignInDate <> '2015-05-05' OR um.SignInDate IS NULL) AND um.UserRoles = 'RCO'))



but i am having this error: An explicit value for the identity column in table 'UserSignInHistory' can only be specified when a column list is used and IDENTITY_INSERT is ON.

i have gone through the code, i couldn't fix this error, please can anyone assist?
Posted
Updated 5-May-15 22:04pm
v3
Comments
Er. Ajay Chauhan 6-May-15 3:11am    
use 1 more closing ')' after last line..
Uwakpeter 6-May-15 3:33am    
yes, i have done that, having this error message now: An explicit value for the identity column in table 'UserSignInHistory' can only be specified when a column list is used and IDENTITY_INSERT is ON.

1 solution

The error message actually tells you what's wrong. You are forcing a value into an identity field that usually generates it's own value as most primary key fields do.

You will need to check the table's structure to identify the "identity field" and then arrange you SQL as this:

SQL
INSERT INTO UserSignInHistory ([Username],/*LIST OF FIELDS EXCLUDING IDENTITY FIELD*/)
		SELECT [Username],/*LIST OF FIELDS EXCLUDING IDENTITY FIELD*/
		FROM [UserMgmt] um
		WHERE
        NOT EXISTS (SELECT * FROM UserSignInHistory ss
		
		WHERE ((um.SignInDate <> '2015-05-05' OR um.SignInDate IS NULL) AND um.UserRoles = 'RCO'))
 
Share this answer
 
Comments
Uwakpeter 7-May-15 7:57am    
I want the query to be executing once everyday.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900