all groups > sql server programming > november 2005 >
You're in the

sql server programming

group:

Stored Procedures and the GetTime() function



Stored Procedures and the GetTime() function Grant Smith
11/26/2005 11:15:27 PM
sql server programming: I want to write a conditional statement in which a timestamp in a
database is compared to the current time. Example: If timestamp value is
[quoted text, click to view]

The purpose of this is to verify that data has been imported in to the
table within the last hour.

I figured the GetTime() function would provide me with the current time
but I'm not sure how to set it up so that it evaluates the current time
- 1 hour.

Any help would be greatly appreciated.

Thanks,
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: Stored Procedures and the GetTime() function Erland Sommarskog
11/26/2005 11:47:55 PM
Grant Smith (grant.smith@envent-tech.com) writes:
[quoted text, click to view]

Since there is no GetTime() function in SQL Server, this is a little
confusing. In which environment are you working in and want to write
you conditional statement?

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Stored Procedures and the GetTime() function Grant Smith
11/26/2005 11:49:44 PM
[quoted text, click to view]

I'm sorry. I meant to say GetDate()
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: Stored Procedures and the GetTime() function Mike Epprecht (SQL MVP)
11/27/2005 12:47:45 AM
Hi

SELECT DATEADD(hour, -1, GETDATE())

--
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

[quoted text, click to view]

What am I doing wrong?? Grant Smith
11/27/2005 4:59:06 PM
[quoted text, click to view]

Here's what I coded:

CREATE PROCEDURE [dbo].[gs_VerifyImportLite]
@timeCheck datetime = DateAdd(hour, -1, GetDate())

AS
if exists (SELECT * FROM IMPORT_LITE WHERE import_date >= @timeCheck)
return 0
else
return 1

It keeps giving me syntax errors when I check syntax.

Any ideas?
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: What am I doing wrong?? Grant Smith
11/27/2005 5:06:56 PM
[quoted text, click to view]

I will give this a try. Thanks.
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: What am I doing wrong?? Grant Smith
11/27/2005 5:15:31 PM
[quoted text, click to view]

I actually had to code it like this to get the syntax check to recognize it:

CREATE PROCEDURE [dbo].[gs_VerifyImportLite]

@timeCheck datetime

AS

SET @timeCheck = DateAdd(hour, -1, GetDate())

if exists (SELECT * FROM IMPORT_LITE WHERE Upload_date >= @timeCheck)
return 0
else
return 1

According to what I've been reading, you have to declare variables
before the AS section. Thanks again for your help. I just hope it
works... LOL
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: What am I doing wrong?? Grant Smith
11/27/2005 5:37:01 PM
[quoted text, click to view]

OK. Now I understand. Thanks again.
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: What am I doing wrong?? Mike Epprecht (SQL MVP)
11/27/2005 6:02:56 PM
CREATE PROCEDURE [dbo].[gs_VerifyImportLite]
AS

DECALRE @timeCheck datetime

SET @timeCheck = DateAdd(hour, -1, GetDate())

if exists (SELECT * FROM IMPORT_LITE WHERE import_date >= @timeCheck)
return 0
else
return 1


You can't declare and assign a value in one operation. This is not .NET

Regards

--
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

[quoted text, click to view]

Re: What am I doing wrong?? Mike Epprecht (SQL MVP)
11/27/2005 6:26:08 PM
Hi

Anything before the AS is a parameter for the SP.

Regards
--------------------------------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

[quoted text, click to view]

Re: Stored Procedures and the GetTime() function Grant Smith
11/28/2005 1:49:03 AM
[quoted text, click to view]

OK... So I wrote the following stored procedure and it runs just fine
but I can't seem to get the job to fail on a return of 1. Any ideas?

CREATE PROCEDURE [dbo].[gs_VerifyImportLite]

AS

DECLARE @timeCheck datetime
SET @timeCheck = DateAdd(hour, -1, GetDate())

if exists (SELECT * FROM IMPORT_LITE WHERE Upload_date >= @timeCheck)

return 0

else

return 1
GO

Thanks,
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
Re: Stored Procedures and the GetTime() function Erland Sommarskog
11/28/2005 11:00:46 PM
Grant Smith (grant.smith@envent-tech.com) writes:
[quoted text, click to view]

Did you explain what context the program runs in? Without knowledge of
that, it's difficult to have ideas. :-)

But if you want an error, using RAISERROR may be a better option.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Stored Procedures and the GetTime() function Grant Smith
11/29/2005 2:30:33 AM
[quoted text, click to view]

It is a Stored Procedure running as a SQL Agent Job. I changed the SP to
use RAISERROR. I haven't figured out if it works yet or not because
there hasn't been a situation where I could force a fail... LOL

Thanks,
--
Grant Smith
A+, Net+, MCP x 2

Quality Production Liaison
Hewlett Packard Company

Database Administrator
AddThis Social Bookmark Button