How do I select any value from SP? - sql

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

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

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 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.

SQL Server stored procedure optional parameters, include all if null

I have this stored procedure:
ALTER PROCEDURE [dbo].[GetCalendarEvents]
(#StartDate datetime,
#EndDate datetime,
#Location varchar(250) = null)
AS
BEGIN
SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND (Location IS NULL OR Location = #Location)
END
Now, I have the location parameter, what I want to do is if the parameter is not null then include the parameter in where clause. If the parameter is null I want to completely ignore that where parameter and only get the result by start and end date.
Because when I'm doing this for example:
EXEC GetCalendarEvents '02/02/2014', '10/10/2015', null
I'm not getting any results because there are other locations which are not null and since the location parameter is null, I want to get the results from all the locations.
Any idea how can I fix this?
ALTER PROCEDURE [dbo].[GetCalendarEvents]
( #StartDate DATETIME,
#EndDate DATETIME,
#Location VARCHAR(250) = NULL
)
AS
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = ISNULL(#Location, Location )
END
If a NULL column is a possibility, then this would work.
ALTER PROCEDURE [dbo].[GetCalendarEvents]
( #StartDate DATETIME,
#EndDate DATETIME,
#Location VARCHAR(250) = NULL
)
AS
BEGIN
IF ( #loc IS NULL )
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
END
ELSE
BEGIN
SELECT *
FROM events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = #Location
END
END
As having an 'OR' clause should be reasonably avoided due to possible performance issues.
The part in the WHERE clause should then read
AND (#Location IS NULL OR Location=#Location)
Try this
SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate
AND Location = Case When LEN(#Location) > 0 Then #Location Else Location End
It can be easily done with a dynamic sql query.
ALTER PROCEDURE [dbo].[GetCalendarEvents]
(#StartDate datetime,
#EndDate datetime,
#Location varchar(250) = null)
AS
BEGIN
DECLARE #SQL NVARCHAR(MAX);
DECLARE #PARAMETER_DEFIINITION NVARCHAR(MAX);
DECLARE #WHERE_PART NVARCHAR(MAX);
SET #PARAMETER_DEFIINITION =' #StartDate DATETIME, #EndDate DATETIME, #Location VARCHAR(250) '
SET #SQL ='SELECT *
FROM Events
WHERE EventDate >= #StartDate
AND EventDate <= #EndDate '
IF #Location IS NOT NULL
BEGIN
SET #WHERE_PART = ' AND Location = #Location '
END
SET #SQL = #SQL + #WHERE_PART
EXEC SP_EXECUTESQL #SQL, #PARAMETER_DEFIINITION, #StartDate, #EndDate, #Location
END
Query will be dynamically created according to the parameters. In here if #location is null then it will not add to the where part.
If you want more on writing dynamic queries please refer this article. http://codingpulse.blogspot.com/2015/02/dynamic-sql-in-stored-procedure-part-1.html

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