MSSQL SP dateadd problem...
Posted on 2004-11-22
I bet this is an easy one.. But I`m totally stuck...
1:I have a table dbo.DayReg that is supposed to contain one row of info for each day the system is in use. The first thing that happends when a user logs on is that this SP is run and the returnvalue is captured.
2:If there is no record for today, one should be inserted. If there is a record for today, the DID should be returned.
3: a day is defined as today 00:00:00 to 23:59:59
Whats wrong with this one? It just behaves strange without any logic...
Create procedure [Checkday] AS
Declare @retvalue int
If exists(select * from dbo.DayReg where DS>dateadd(day, -1,getdate()))
set @retvalue=(select top 1 DID from dbo.DayReg)
If NOT exists (select * from dbo.DayReg where DS>dateadd(day, -1,getdate()))
insert into dbo.DayReg (Week) values (datepart(week,getdate()))