SQL Server : combine 2 stored procedures into one - sql

I have 2 stored procedures, and my goal is to combine these into one so
I can use filter to select value CHOOSE.
Thank you.
EXEC dbo.newcombine #startdate = 'yyyy-mm-dd', #enddate = 'yyyy-mm-dd', #choose = 'testone' OR #choose = 'testwo'
First stored procedure:
#choose nvarchar(10)
set #choose = testone
#startdate date
,#enddate date
AS
BEGIN
SET NOCOUNT ON;
SELECT
c.TypeofCall
FROM
Contact c
AND c.callin BETWEEN #startdate AND #enddate
END
Second stored procedure:
#choose nvarchar(10)
set #choose = testtwo
#startdate date
,#enddate date
AS
BEGIN
SET NOCOUNT ON;
SELECT
c.TypeofService
FROM
Contact c
AND c.callin BETWEEN #startdate AND #enddate
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

SQL SERVER - Need a Simple T-SQL Program to iterate date

My procedure that I am calling works on day by day basis. I need to pass/iterate by one day upto the current date to the procedure for it to perform the action. Can some help with that. Here is the sample that I tried but it fails to work. I am beginner.
USE [aaa]
GO
DECLARE #return_value int
DECLARE #VarDate1 Datetime ='2015-09-30'
DECLARE #VarDate2 Datetime ='2015-09-30'
WHILE (#VarDate2 <= '2015-12-10')
BEGIN
EXEC #return_value = [dbo].[procname]
#tday1 = #VarDate1 ,
#tday2 = #VarDate2
SET #VarDate2 = DATEADD(DAY, 1, #VarDate2)
set #VarDate1 = #VarDate2
SELECT 'Return Value' = #return_value
END
GO
By checking your code, you're probably getting an error while trying to set the value for #VarDate1 and #VarDate2. Try the code below:
DECLARE #return_value int
DECLARE #VarDate1 Datetime = CAST('20150930 00:00:00' AS DATETIME)
DECLARE #VarDate2 Datetime = CAST('20150930 00:00:00' AS DATETIME)
WHILE (#VarDate2 <= '20151210') BEGIN
SET #VarDate2 = DATEADD(DAY, 1, #VarDate2)
SET #VarDate1 = #VarDate2
PRINT #VarDate2 --Change your logic here
END
GO

Need to set a one-off date range for SQL query

I have here part of a working script that runs to retrieve data from <yesterday> as shown here:
-- Insert statements for procedure here
DECLARE #beginDate datetime, #endDate datetime, #itemCount int, #total decimal(10,2)
SET #beginDate = DATEADD(day,DATEDIFF(day,1,GETDATE()),0)
SET #endDate = DATEADD(day,DATEDIFF(day,0,GETDATE()),0)
--PRINT #beginDate
--PRINT #endDate
I want to run this for a one-off result to collect data that was missed during a server migration.
I have tried this:
-- Insert statements for procedure here
DECLARE #beginDate datetime, #endDate datetime, #itemCount int, #total decimal(10,2)
SET #beginDate = '11/11/2015'
SET #endDate = '11/30/2015'
--PRINT #beginDate
--PRINT #endDate
But it did not seem to work properly. I wonder if I have the #beginDate and #endDate formatted correctly. Please advise.
Try
SET #beginDate = '20151111'
SET #endDate= '20151130'

Changing Parameter Values in Stored Procedures

I have a stored procedure for a view that is so massive it always times out, it is used to find data for certain date ranges. This is an entirely new concept to me, I have the stored procedure set up for the main date range, I just cant figure out how to Execute it properly if I need specific dates. Here is the code and issue
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[COL_Run_DOM_Parameters]
#StartDate varchar (50),
#EndDate varchar (50)
AS
SET NOCOUNT ON
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN '2010-01-01' AND '2012-12-31'
When I execute I do it like:
Execute COL_Run_DOM_Parameters #StartDate = '2011-12-22', #EndDate '2012-05-17'
But when I execute it still gives me all the data between 2010 and 2012 instead of the date range I asked for. Where in my code is there a mistake?
You need to change your query to reference the parameters!
ALTER PROCEDURE [dbo].[COL_Run_DOM_Parameters]
#StartDate varchar (50),
#EndDate varchar (50)
AS
SET NOCOUNT ON
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN #StartDate and #EndDate
Execute just like you have been.
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN '2010-01-01' AND '2012-12-31'
you have hardcoded the dates my friend , you are not using your variables
Change the query to
SELECT *
FROM dbo.COL_V_GEMS_DOM_FCT
WHERE REC_EFF_STT_DT BETWEEN #StartDate AND #EndDate
and call the SP like
Declare #StartDate = '2012-02-01'
Declare #EndDate = '2013-02-01'
EXEC COL_Run_DOM_Parameters #StartDate #EndDate

How do I select any value from SP?

I have SP like :
CREATE PROCEDURE MySP
(
#startdate datetime = null,
#enddate datetime = null
)
AS
BEGIN
declare #date datetime
Set #date= convert(datetime,convert(varchar(10),getdate(),101))
SET #startdate = ISNULL(#startdate,convert (datetime,convert(varchar(10),getdate(),101)))
select #startdate -- i want to select and view this value
END
GO
I want to view select #startdate value, How can i do this?
You execute the stored procedure.
exec MySP
Result:
(No column name)
2011-08-10 00:00:00.000
Edit
Stored procedure with output parameter #startdate
alter PROCEDURE MySP
(
#startdate datetime = null out,
#enddate datetime = null
)
AS
BEGIN
declare #date datetime
Set #date= convert(datetime,convert(varchar(10),getdate(),101))
SET #startdate = ISNULL(#startdate,convert (datetime,convert(varchar(10),getdate(),101)))
END
Use like this
declare #D datetime
exec MySP #D out
select #D