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
 General SQL Server Forums
 New to SQL Server Programming
 SP GetDate Syntax Error

Author  Topic 

yawnzzzz
Starting Member

13 Posts

Posted - 2011-06-16 : 12:00:23
Hi,

I'm creating a Store Procedure where the user will be submitting some information along with a date. If the date isn't submitted, then I want to use the current date, but when I try to do this it throw an error: Incorrect Syntax near ')'.

CREATE PROCEDURE [user_rw].[UpdateSomething]
@SomeID int out,
@EffectiveDate datetime
AS
BEGIN
SET NOCOUNT ON;
IF @EffectiveDate is NULL
BEGIN
SET @EffectiveDate = GetDate(); --Error here
END
BEGIN
--Other Code
END
END

yawnzzzz
Starting Member

13 Posts

Posted - 2011-06-16 : 12:02:46
Sorry, nevermind. Error was somewhere else, and it just happened to be pointing at that line by mistake.
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2011-06-17 : 01:52:58
CREATE PROCEDURE [user_rw].[UpdateSomething]
@SomeID int out,
@EffectiveDate datetime
AS
BEGIN
SET NOCOUNT ON;
IF @EffectiveDate is NULL
BEGIN
SET @EffectiveDate = GetDate(); --Error here
END
Else
BEGIN
--Other Code
END
END


Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-06-20 : 12:15:20
The condition
IF @EffectiveDate is NULL
BEGIN
SET @EffectiveDate = GetDate(); --Error here
END

can be simplified to

set @EffectiveDate =coalesce(EffectiveDate,getdate())

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jeffw8713
Aged Yak Warrior

819 Posts

Posted - 2011-06-20 : 15:46:59
I would also make the parameter optional...

CREATE PROCEDURE [user_rw].[UpdateSomething]
@SomeID int out,
@EffectiveDate datetime = NULL
AS


And, if you are passing in a date with time and don't want the time:

SET @EffectiveDate = dateadd(day, datediff(day, 0, coalesce(@EffectiveDate, getdate())), 0);

Jeff
Go to Top of Page
   

- Advertisement -