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
 Transact-SQL (2005)
 INSERT statement

Author  Topic 

JeffS23
Posting Yak Master

212 Posts

Posted - 2007-12-28 : 17:09:14
The following script will not run, but I need to do something like this. I have a table in SQL that holds Employee Leave transactions. I need to add lines to that table via a SQL script on the frist of the month. I can schedule a job to do that, but the script won't run. My problem is the EmpLeaveId field. It is a number that is auto incremented by the HR program that puts the remaining data in this table. How can I insert a number into my row that has is the max value thus far plus 1? This is an example script below that I was trying to use to insert one record only into the table. If I can get this to work, I can automate it from there. Thanks!!

insert into EmpLeaveTemp (EmpUID, CompanyID, EmpLeaveId, LeaveTypeId, StartDate, EndDate, LeaveQty, LeaveAdType, LstModBy, LstModDate, Notes)
Values (20, 6, select MAX(EmpLeaveId)+1 from EmpLeave, 17, 01/01/2008, 01/01/2008, 8.0000, 'A', 'Massey', GetDate(), 'Automated Personal')

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-12-28 : 17:14:19
can you have EmpLeaveId as identity field?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-28 : 17:46:44
Just in case he can't use identity:

insert into EmpLeaveTemp (EmpUID, CompanyID, EmpLeaveId, LeaveTypeId, StartDate, EndDate, LeaveQty, LeaveAdType, LstModBy, LstModDate, Notes)
select 20, 6, MAX(EmpLeaveId)+1, 17, '01/01/2008', '01/01/2008', 8.0000, 'A', 'Massey', GetDate(), 'Automated Personal'
from EmpLeave

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -