Changing Parameter Values in Stored Procedures - sql

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

Related

SQL Server : combine 2 stored procedures into one

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

How can I create a stored procedure which takes values from a table and insert them into new table

How to create a stored procedure in SQL which accepts name,area,startdate,enddate as inputs from a table and inserts name,area and date into a table(having 3 columns name,area,date) for every date between startdate and enddate.
This is not possible. You cannot perform DDL and DML operations in functions in SQL Server.
As Gordon suggested, you need to use a stored procedure to achieve this.
Here is an example to start with
http://www.sqlinfo.net/sqlserver/sql_server_stored_procedure_INSERT.php
DECLARE #name NVARCHAR(20) = 'name';
DECLARE #area NVARCHAR(20) = 'area';
DECLARE #startDate DATE = '2016-11-30';
DECLARE #endDate DATE = '2016-12-30';
DECLARE #currentDate DATE = #startDate;
SELECT
#currentDate AS Date
INTO
#dates;
WHILE(#currentDate < #endDate)
BEGIN
SET #currentDate = DATEADD(d, 1, #currentDate);
INSERT INTO #dates
VALUES( #currentDate);
END;
SELECT
#name,
#area,
*
FROM
#dates;
You can get variables as parameter and use the last select statement to create your new table.

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'

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

Calculating variances on dates in T-SQL

Guys, I am trying to write a stored procedure in T-SQL (SQL Server) that will select records based on a date field, keeping a variance of minutes in mind. Something like this:
CREATE PROCEDURE spGetCustomers(#DateRange DATETIME, #Variance int) AS
-- The next line is where I need help
-- I'm trying to subtract X amount of minutes from the date
-- So if #Variance = 4 AND #DateRange = '6/10/2009 1:15pm'
-- Then #StartDate should equal '6/10/2009 1:11pm'
DECLARE #StartDate = #DateRange - #Variance
-- I also need an #EndDate, which will be X amount of minutes
-- in the future. So if #Variance = 4 AND #DateRange = '6/10/2009 1:15pm'
-- Then #EndDate should equal '6/10/2009 1:19pm'
DECLARE #EndDate = #DateRange + #Variance
SELECT * FROM Customers WHERE Created BETWEEN #StartDate AND #EndDate
Hopefully this makes sense and someone can help me out! Thanks in advance
Check this out:
http://msdn.microsoft.com/en-us/library/ms186819(SQL.90).aspx
The DATEADD function allows you to add virtually any part of a date to another date object, it should be everything you need.
So basically do:
SELECT DATEADD(second, #Variance, #DateRange)
The following script provides an example that should get you started.
create table tmp_Customers
(
ID int identity(1,1),
CreatedDate datetime default getDate() not null,
Description varchar(15)
);
go
insert into tmp_Customers(Description) values('SomeData');
insert into tmp_Customers(Description) values('SomeData2');
insert into tmp_Customers(Description) values('SomeData3');
go
create procedure usp_GetCustomers
#iVarianceMinutes int,
#iDateRange datetime
as
set nocount on
declare #startDate datetime
declare #endDate datetime
--Define the date ranges for the select query
set #startDate = dateAdd(minute,-#iVarianceMinutes,#iDateRange)
set #endDate = dateAdd(minute,#iVarianceMinutes,#iDateRange)
--Get the Customers that were created within this time range.
SELECT *
FROM tmp_Customers
WHERE CreatedDate >= #startDate and CreatedDate < #endDate
return(0);
go
--Execute the procedure
declare #testDate datetime;
set #testDate = getDate();
exec usp_GetCustomers 5,#testDate
--drop procedure usp_GetCustomers
--drop table tmp_Customers