Setting default parameters for a SQL Server stored procedure - sql

I am trying to set the default value of #Day to the current day. However, I am getting a couple errors on the initial DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0) and on all the #Days below it. I thought that the #Day DateTime under the alter procedure declared #Day but I am getting the error:
Must declare the scalar variable "#Day"
Anyone know what to do?
ALTER PROCEDURE [dbo].[InsertWeeklyRuns]
#Day DateTime = DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
AS
BEGIN
SET NOCOUNT ON
DECLARE #Enddate DATETIME
DECLARE #StartDate DATETIME
DECLARE #F1Runs INT
DECLARE #F2Runs INT
DECLARE #F3Runs INT
DECLARE #F1Alarms INT
DECLARE #F2Alarms INT
DECLARE #F3Alarms INT
SET #Day = DATEADD(dd, DATEDIFF(dd, 0, #Day), 0)
SET #Enddate = CASE
WHEN #Day > DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
THEN DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
ELSE DATEADD(Day, 1, #Day)
END
SET #StartDate = #Enddate - 7
Thanks

This should work as you expected:
CREATE PROCEDURE [dbo].[InsertWeeklyRuns]
#Day DateTime
AS
BEGIN
SET NOCOUNT ON
Declare #Enddate Datetime
Declare #StartDate Datetime
Declare #F1Runs Int
Declare #F2Runs Int
Declare #F3Runs Int
Declare #F1Alarms Int
Declare #F2Alarms Int
Declare #F3Alarms Int
DECLARE #today date = CAST(GETDATE() AS date);
IF #Day IS NULL
Set #Day = #today
Set #Enddate = CASE WHEN #Day >= #today THEN #today ELSE DATEADD(Day, 1, #Day) END
Set #StartDate = #Enddate - 7
END

Related

Trying to set default param value in stored procedure but errors on CONVERT

Wish to set default value in stored procedure but running into error, CONVERT doesn't seem to exist. CONVERT works in the body but not in the params section of the stored procedure. How do I set param #StartDate to a default of 24 months in the past?
CREATE PROCEDURE [dbo].[MyStoredProc]
#Id INT = NULL,
#startDate DATETIME = CONVERT(DATE, DATEADD(MONTH, -1 * 24, GETDATE())), -- last 24 months
#endDate DATETIME = CONVERT(DATE, GETDATE())
AS
BEGIN
SELECT #startDate;
END
GO
That works!
CREATE procedure [dbo].[MyStoredProc]
#Id int = null
,#startDate datetime = null
,#endDate datetime = null
as
begin
SET #startDate = ISNULL(#startDate, CONVERT(DATE, DATEADD(MONTH, -1*24, GETDATE())));
select #startDate;
END
GO

How to declare a paramater with default value (previous date) in a Stored Procedure?

I want to set default params in a stored procedure as follows:
ALTER PROCEDURE [rpt].[STAT05] --#StartDate = '2017-08-15', #EndDate = '2017-08-16'
#StartDate DATETIME2 = DATEADD(DAY, -2, GETDATE())
,#EndDate DATETIME2 = DATEADD(DAY, -1, GETDATE())
AS
BEGIN
,which however returns:
Must declare the scalar variable "#StartDate".
What's wrong with that statement?
Default value for parameters in sp have to be constants.
ALTER PROCEDURE [rpt].[STAT05]
#StartDate DATETIME2 = null
,#EndDate DATETIME2 =null
AS
BEGIN
IF #StartDate is null
SET #StartDate = DATEADD(DAY, -2, GETDATE())
IF #EndDate is null
SET #EndDate = DATEADD(DAY, -1, GETDATE())

Conditionally setting a variable in SQL

I'm trying to do this:
If the Day parameter is the current day, then set #EndDate to be yesterday instead
If the Day parameter is in the future, set #EndDate to yesterday.
I've tried a few different approaches including two down below. I'm fairly new to programming so odds are I'm missing something fairly simple. Basically, I am trying to set #EndDate conditionally, depending on what #Day is set to be.
DECLARE #Day DATETIME
SET #Day = '09/2/17 12:50'
SET #Day = DATEADD(dd, DATEDIFF(dd, 0, #Day), 0)
DECLARE #Enddate DATETIME
SET #Enddate = CASE #Day
WHEN #Day < GETDATE() THEN GETDATE() - 1
END
--SET #Enddate = #Day
-- WHERE #Day < GETDATE()
--SET #Enddate = GETDATE()-1
-- WHERE#Day >= GETDATE()
Thanks
You are mixing the two possible ways to write a case expression...
You can either use
CASE #day
WHEN GETDATE() THEN 'x'
WHEN DATEADD(DAY, 1, GETDATE() THEN 'y'
END
or then you can use:
CASE
WHEN #day = GETDATE() THEN 'x'
WHEN #day > GETDATE() THEN 'y'
END
This is the correct way:
SET #Enddate = CASE
WHEN #Day < GETDATE()
THEN DATEADD(DAY, -1, GETDATE())
ELSE GETDATE()
END
For clarification, variable #yesterday added as DATE without time
Declare #Day datetime
Set #Day = '09/02/17 12:50'
SET #Day = DATEADD(dd, DATEDIFF(dd, 0, #Day), 0)
Declare #Enddate Datetime
Declare #yesterday as date
SET #yesterday=dateadd(day,-1,getdate())
Set #Enddate = Case
When #day< #yesterday Then #day else #yesterday End
Any values older than yesterday remains as is, other case sets yesterday

Stored Procedure throwing error

I tried to execute this procedure but I am getting error.
I tried to execute using:
execute Currentmonth 20141220
Error:
Msg 208, Level 16, State 1, Procedure Currentmonth, Line 15
Invalid object name 'DimDate'
Why I am getting this error? Can you please tell me the errors in the query for creating stored procedure and what are the parameters I am expecting?
create procedure Currentmonth
#Completeddatekey varchar(20)
as
begin
Getting the current date and formatting it
Declare #currentdate varchar(30)
set #currentdate = convert(Varchar(20), getdate()-1, 101)
print #currentdate
Getting DayofMonth and EndofMonth from DimDate
Declare #dayofmonth int
Declare #endofmonth int
select #dayofmonth = DayofMonth, #endofmonth = EndofMonthDateKey
from bi.dbo.DimDate
where datekey = #currentdate
Getting HierMonthEndKey
declare #hiermonthendkey int
select #hiermonthendkey = MAX(HierMonthEndKey)
from DimHospiceHiearchy
where HierMonthEndKey <= #currentdate+1
Declare #day
For Loop
Declare #i int = 0
declare #startdate varchar(20)
select #startdate = CAST(CAST(YEAR(convert(Varchar(20), getdate()-1, 101)) AS VARCHAR(4))
+ '/' + CAST(MONTH(convert(Varchar(20), getdate()-1, 101)) AS VARCHAR(2)) + '/01' AS DATETIME)+1
While #i <=#dayofmonth
begin
set #startdate = #startdate+#i
exec MA010103 #completeddatekey, #hiermonthendkey
set #i = #i+1
end
end
This error occurs when an object that does not exist is referenced. If the object exists, you might need to include the owner's name in the object name.
Please check the table exists in the mentioned database ?
select #dayofmonth = DayofMonth, #endofmonth = EndofMonthDateKey from
bi.dbo.DimDate
where datekey = #currentdate
Instead of
Declare #dayofmonth int
Declare #endofmonth int
select #dayofmonth = DayofMonth, #endofmonth = EndofMonthDateKey from bi.dbo.DimDate
where datekey = #currentdate
Please try
Declare #dayofmonth int
Declare #endofmonth int
select #dayofmonth = DayofMonth, #endofmonth = EndofMonthDateKey from DimDate
where datekey = #currentdate
Or check if DimDate Table exists

SQL convert DATETIME TO VARCHAR?

I am working on a query something require DATE!!
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL
#DateFr is varchar(50)
#DateT0 is varchar(50)
the #dateFr and #dateTo are both varchar..
And I try to execute it, it print the time format as this 2011-06-09 16:15:38.927
error statement
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Additionally, the varchar format I need is MM-DD-YYYY
Anyone know where is my error at?
thanks
Your code is confusing:
DECLARE #YesterDay DATETIME, #Today DATETIME
SET #YesterDay = DateAdd(DD, DateDiff(DD, 0, GETDATE())-1, 0)
SET #Today = DateAdd(DD, DateDiff(DD, 0, GETDATE()), 0)
select #YesterDay = convert(varchar, getdate()-1 , 110)
select #Today = convert(varchar, getdate() , 110)
So you declare it as DATETIME, set value with DateDiff then overwrite that value with an varchar representation of a date that is recalculated using different method.
At line 4 and 5 #Yesterday and #Today variables are still DATETIME, because it's declared that way.
EDIT: As mentioned in the comment to my answer you need a variable to pass to the procedure.
So the correct fix would be to declare the variables as VARCHAR(50) at the beginning and do the conversion directly.
DECLARE #YesterDay VARCHAR(50), #Today VARCHAR(50)
SELECT #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
SELECT #Today = convert(varchar(50), getdate() , 110)
And then call the procedure the way you did originally.
My guess is that getdate()-1 part is wrong.
Also, you were making dateadd and datediff why? Try it like this:
SET #YesterDay = dateadd(dd, -1, GETDATE())
SET #Today = GETDATE()
select #YesterDay = convert(varchar(50), dateadd(dd, -1, getdate()) , 110)
select #Today = convert(varchar(50), getdate() , 110)
EXEC #return_value = [dbo].[post_sec_admin_list_user_log]
#pDateFr = #YesterDay ,
#pDateTo = #Today,
#pName = '',
#pSec = NULL