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)
 to insert..or not to insert..that is the question.

Author  Topic 

dlorenc
Posting Yak Master

172 Posts

Posted - 2009-01-29 : 15:01:31
I am adding to a table on a weekly basis with a sql job script. The data source could be new, could be an update, or could be the same as last week. I would like to modify my 'insert' logic to check to see if that weeks data is already there and just update the existing record rather than inserting a new record. The combined key to know if the record exists is MetricID & ReportWeek (there should be only one record for a metric in a certain reportweek.)

Insert scorecard.scorecardmetrics(MetricID,Reportweek,Target,Value1,Value2,MetricValue,Entered)

SELECT 40 as MetricID --
,DATEADD(d, - DATEPART(dw, GETDATE()), GETDATE()) AS ReportWeek
,.8 as Target
,COUNT(AgencyCode) AS Value1
,15 as Value2
,COUNT(*) / 15. AS MetricValue
,GETDATE() AS Entered
FROM Confirm
WHERE (Action = 'fy-10 Budget') AND (Confirmed = 1)

If I can make the decision to update, rather than insert, then GETDATE() will update field 'Update' rather then setting the 'Entered' field.

Thanks for the help!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-29 : 15:48:07
Donot CrossPost:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=118855
Go to Top of Page
   

- Advertisement -