MSSQL SP dateadd problem...

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...
W2K3+MSSql2K

__________________________________________________
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()))
select @@identity
set @retvalue=@@identity

return @retvalue
Go
_____________________
LVL 15
mattisflonesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muzzy2003Commented:
You need to use the following code, as at the moment you are comparing the previous 24 hour period, not just the same day. Also, your three commands after the second IF all need to be grouped with BEGIN ... END.

Create procedure [Checkday] AS

Declare @retvalue int

If exists(select * from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)
set @retvalue=(select top 1 DID from dbo.DayReg)

If NOT exists (select * from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)
begin
      insert into dbo.DayReg (Week) values (datepart(week,getdate()))
      select @@identity
      set @retvalue=@@identity
end

return @retvalue
Go

Or, a little neater:

Create procedure [Checkday] AS

Declare @retvalue int

select @retvalue = * from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)

IF @retvalue IS Null
begin
      insert into dbo.DayReg (Week) values (datepart(week,getdate()))
      select @@identity
      set @retvalue=@@identity
end

return @retvalue
Go
0
mattisflonesAuthor Commented:
Hi, thanx for helping.. I see where you are going with this.. and it makes sense!

But... While testing your second approach i get
Error 116: Only one expression can be specified in the select list when the sub query is not introduced with EXISTS"

And there seems to be something wrong here:

"select @retvalue = * from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)"

Isn`t set the correct way to set a variable?!
0
muzzy2003Commented:
Sorry - should be:

select @retvalue = DID from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)

It's only 7 am here, and I'm groggy!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mattisflonesAuthor Commented:
This one worked!

________________________________
Create procedure [Checkday] AS

Declare @retvalue int
If Exists (select * from dbo.DayReg where DATEDIFF(day, DS, GETDATE()) = 0)
Begin
      set @retvalue =(select top 1 DID from dbo.DayReg order by DS Desc)
End

IF @retvalue IS Null
begin
     insert into dbo.DayReg (Week) values (datepart(week,getdate()))
     select @@identity
     set @retvalue=@@identity
end

return @retvalue
Go
_______________________
0
mattisflonesAuthor Commented:
Awright.. Now yours works too...

Thanx for the help! Have a nice morning...
0
muzzy2003Commented:
No problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.