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 2000 Forums
 Transact-SQL (2000)
 Using insert and update

Author  Topic 

dougancil
Posting Yak Master

217 Posts

Posted - 2011-02-09 : 14:49:17
I have the following query:

insert into payroll (Employeenumber, TotalRegHours, TotalOT)
UPDATE Payroll SET payrolldate = CURRENT_TIMESTAMP, payrollran = 'Yes',
approved = 'Yes'

but I'm getting a syntax error near Update. Can someone please advise?

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-02-09 : 15:00:03
What are you trying to do? An insert or an update?

You haven't specified anything to insert.
insert into payroll (Employeenumber, TotalRegHours, TotalOT)
VALUES(value1, value2, value3);


Are you trying to update every row? Because that is what the update you have will do.
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2011-02-09 : 15:08:40
actually what I'm trying to do is select all from a table I already have, insert it into my payroll table and then update. Is that possible?

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-09 : 15:26:54
insert into payroll (Employeenumber, TotalRegHours, TotalOT)
select ... from yourExistingTable

UPDATE Payroll SET payrolldate = CURRENT_TIMESTAMP, payrollran = 'Yes',
approved = 'Yes'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2011-02-09 : 15:34:47
Tara,

Thank you but I just noticed something, when I update payrolldate, I don't need to update the entire column. I guess what I need to check for is if there's a date in there already, and then update the new information with the current timestamp.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-09 : 15:38:59
Use CASE for that then:

CASE WHEN payrolldate IS NULL THEN CURRENT_TIMESTAMP ELSE payrolldate END

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2011-02-09 : 16:08:02
Here's what I have:

insert into payroll (Employeenumber, TotalRegHours, TotalOT)
select * from scratchpad7
Update Payroll
CASE WHEN payrolldate IS NULL THEN CURRENT_TIMESTAMP ELSE payrolldate END, payrollran = 'Yes',
approved = 'Yes'
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-09 : 16:25:59
insert into payroll (Employeenumber, TotalRegHours, TotalOT)
select * from scratchpad7

Update Payroll
SET payrolldate = CASE WHEN payrolldate IS NULL THEN CURRENT_TIMESTAMP ELSE payrolldate END,
payrollran = 'Yes',
approved = 'Yes'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dougancil
Posting Yak Master

217 Posts

Posted - 2011-02-09 : 16:36:26
Thank you Tara.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-09 : 16:41:29
You're welcome, glad to help.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -