ms sql call stored procedure with date time parameter - sql

how do one call a stored procedure that has date input .
spName getDate()
does not work.
the question is about calling within ms sql managment studio.

SQL Server 2008
declare #d date = getdate() /*Or datetime looking at the title*/
exec spName #d
Earlier Versions
declare #d datetime
set #d = getdate()
exec spName #d

Related

How to pass GETDATE (Only date) as a parameter in sql server?

I want to pass the system date (GETDATE) only as a parameter in my SQL stored procedure. But I am getting an error while executing the procedure.
SQL query:
ALTER PROCEDURE ST_PRO_GETUSER
#D DATETIME = GETDATE --// passing GETDATE as a parameter.
AS BEGIN
select case when branch in ('A25','B10','C10')
then 'BR B1' Else 'BR B2'
end As [COLLECTION],FIXDATE
from MAIN_COUNTER where TDATE=#D --//Just want to pass date only
group by COLLECTION,FIXDATE
END
EXEC KK_SP_GETUSER_DIV
Error:
Conversion failed when converting date and/or time from character string.
What I have to do for it?
To pass as a parameter you just declare a variable and pass it in:
DECLARE #DATE DATETIME = GETDATE();
EXEC ST_PRO_GETUSER #DATE;
And if you want the date only, change the datatype of your parameter to a date and then do:
DECLARE #DATE DATE = GETDATE();
EXEC ST_PRO_GETUSER #DATE;
But part of your question seems to actually be asking how to specify a default parameter value. You can't use a function for the default value, so instead do:
CREATE PROCEDURE ST_PRO_GETUSER
(
#Date DATETIME = null
-- Change to DATE datatype if you don't want a time component.
-- #Date DATE = null
)
AS
BEGIN
SET NOCOUNT ON;
-- Default the #Date here is its null.
-- Note this doesn't handle the case when the caller wants to pass in null.
SET #Date = COALESCE(#Date,GETDATE());
-- SP Body
RETURN 0;
END
Solved My Self
ALTER PROCEDURE ST_PRO_GETUSER
#Date datetime = null
as
IF #Date is null
SET #Date = getdate() --// passing GETDATE as a parameter.
BEGIN
select case when branch in ('A25','B10','C10')
then 'BR B1' Else 'BR B2'
end As [COLLECTION],FIXDATE
from MAIN_COUNTER where TDATE=#D --//Just want to pass date only
group by COLLECTION,FIXDATE
END
EXEC ST_PRO_GETUSER
GETDATE() is the correct syntax, not GETDATE.

How to pass parameter as date by subtracting days to a SQL stored procedure?

I have a stored procedure which takes two parameters, #FromDate and #ToDate. But the #fromDate should be GetDate()-90. How can I pass the parameter value?
ALTER PROCEDURE spMyProcedure
#FromDate DATE,
#ToDate DATE
AS
BEGIN
// SQL statements
END
I want to execute it like this:
EXEC spMyProcedure #FromDate = GETDATE() - 60, #ToDate = GETDATE()
But it throws an error.
You can not do an alter/math/etc on a value passing to a stored procedure. You can declare a date above the stored procedure and then pass that value like this:
DECLARE #PassDate AS DATETIME
SET #PassDate = GETDATE() - 60
EXEC spMyProcedure #FromDate = #PassDate, #ToDate = GETDATE()
I suspect you are working with SQL Server if so, then use dateadd() function inside the stored procedure
set #FromDate = dateadd(day, -90, getdate())

SQL Server 2012 stored procedure runs slow

I have a SQL Server stored procedure and it runs fine on SQL Server 2008 R2.
When I try to run it on SQL Server 2012, it takes very very long time to run.
But if I create local variables inside the stored procedure and copy the values of input parameters into those local variables and use them instead of input parameters, query runs and returns result faster than on SQL Server 2008 R2 database (please note both 2008 R2 and 2012 servers run on the same box).
Could you please shed some light on what is going on here?
By creating local variables and rebinding values you disable parameter sniffing:
Parameter sniffing is the process whereby SQL Server creates an
optimal plan for a stored procedure by using the calling parameters
that are passed the first time a stored procedure is executed
Every subsequent call to the same store procedure with the same parameters will also get an optimal plan, whereas calls with different parameter values may not always get an optimal plan
Run slows:
CREATE PROC [dbo].[DisplayBillingInfo]
#BeginDate DATETIME,
#EndDate DATETIME
AS
BEGIN
SELECT BillingDate, BillingAmt
FROM BillingInfo
WHERE BillingDate between #StartDate AND #StopDate;
END
Run fast (because it has to calculate new execution plan each time):
CREATE PROC [dbo].[DisplayBillingInfo]
#BeginDate DATETIME,
#EndDate DATETIME
AS
BEGIN
DECLARE #StartDate DATETIME = #BeginDate;
DECLARE #StopDate DATETIME = #EndDate;
SELECT BillingDate, BillingAmt
FROM BillingInfo
WHERE BillingDate between #StartDate AND #StopDate;
END
The case is SQL Server optimizer cannot reuse cached plan and evaluate it each time.
This is the same as you use WITH RECOMPILE:
CREATE PROC [dbo].[DisplayBillingInfo]
#BeginDate DATETIME,
#EndDate DATETIME
WITH RECOMPILE
AS
BEGIN
SELECT BillingDate, BillingAmt
FROM BillingInfo
WHERE BillingDate between #StartDate AND #StopDate;
END
using query hint:
CREATE PROC [dbo].[DisplayBillingInfo]
#BeginDate DATETIME,
#EndDate DATETIME
AS
BEGIN
SELECT BillingDate, BillingAmt
FROM BillingInfo
WHERE BillingDate between #StartDate AND #StopDate
OPTION(RECOMPILE);
-- OPTION (OPTIMIZE FOR (#StartDate UNKNOWN, #StopDate UNKNOWN))
END

User GETDATE() to put current date into SQL variable

I'm trying to get the current date into a variable inside a SQL stored procedure using the following commands
DECLARE #LastChangeDate as date
SET #LastChangeDate = SELECT GETDATE()
This gives me the following error: "Incorrect Syntax near 'SELECT'"
This is the first stored procedure I've ever written, so I'm unfamiliar with how variables work inside SQL.
You don't need the SELECT
DECLARE #LastChangeDate as date
SET #LastChangeDate = GetDate()
Just use GetDate() not Select GetDate()
DECLARE #LastChangeDate as date
SET #LastChangeDate = GETDATE()
but if it's SQL Server, you can also initialize in same step as declaration...
DECLARE #LastChangeDate date = getDate()
DECLARE #LastChangeDate as date
SET #LastChangeDate = GETDATE()
SELECT #LastChangeDate = GETDATE()
You can also use CURRENT_TIMESTAMP for this.
According to BOL CURRENT_TIMESTAMP is the ANSI SQL euivalent to GETDATE()
DECLARE #LastChangeDate AS DATE;
SET #LastChangeDate = CURRENT_TIMESTAMP;

HH:MM:SS:Msec to HH:MM:SS in stored procedure

I have a stored procedure which update a table based on such calculation and the calculation is done as column name (Calendatedate) - (Current System Date Time) and update this information to a column (TimeSpent) and display the value in Hh:Mm:SS:Msec format.
The query is working fine but I want to update it in such a way so that the time spent should be only HH:MM:SS format. Please help me that how I remove that Msec from the time spent.
CREATE procedure St_Proc_UpdateTimeSpent
#timeEntryID int,
#status int output
as begin
set nocount on;
declare #Date dateTime;
set #Date=GETDATE();
update Production set TimeSpent=(SELECT CONVERT(VARCHAR(20),DateAdd(SS,Datediff(ss,CalendarDate, #Date)%(60*60*24),0),114)),
IsTaskCompleted=1
where productionTimeEntryID=#timeEntryID
set #status=1;
return #status;
end
You can just use style 108 instead of 114 in the CONVERT function to get only the hh:mm:ss:
CREATE PROCEDURE dbo.St_Proc_UpdateTimeSpent
#timeEntryID int,
#status int output
AS BEGIN
SET NOCOUNT ON;
DECLARE #Date DATETIME;
SET #Date = GETDATE();
UPDATE dbo.Production
SET TimeSpent = CONVERT(VARCHAR(20), DATEADD(SS, DATEDIFF(ss, CalendarDate, #Date)%(60*60*24),0), 108),
IsTaskCompleted = 1
WHERE
productionTimeEntryID = #timeEntryID
SET #status = 1;
RETURN #status;
END
See the excellent MSDN documentation on CAST and CONVERT for a comprehensive list of all supported styles when converting DATETIME to VARCHAR (and back)
BTW: SQL Server 2008 also introduced a TIME datatype which would probably be a better fit than a VARCHAR to store your TimeSpent values ... check it out!