Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stored Procedure Loop Throug Date Range

Posted on 2014-04-02
4
Medium Priority
?
2,124 Views
Last Modified: 2014-04-02
I am running a stored procedure which returns and execute on a single date but is there a way to loop through the stored procedure to call a date range instead versus I manually entering 1 date at a time?  Thanks!


USE [Dev]

--Run the Appraisal proc passing in a few required parameters 
Declare @ReportData varbinary(max)
Exec dbo.pAppraisal
@ReportData = @ReportData out, 
@Portfolios = '1234567', 
@Date = '2014-3-31',
@IncludeUnsupervisedAssets = 1,
@IncludeClosedPortfolios = 1

Select c.PortfolioBaseCode, c.ReportHeading1
, a.SecTypeCode, b.Symbol, b.FullName
, a.MarketValue, vs.IsUnsupervised
From dbo.fAppraisal(@ReportData) a
join dbo.vSec b on a.SecurityID = b.SecurityID
join dbo.vSecVariant vs on vs.SecurityID = a.SecurityID AND vs.IsShort = a.IsShortPosition AND vs.SecTypeCode = a.SecTypeCode
join dbo.vPortBase c on a.PortfolioBaseID = c.PortfolioBaseID

Open in new window

0
Comment
Question by:KANEDA 0149
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Jim P. earned 2000 total points
ID: 39972753
Try something like this. You'll have to build the #TempTbl field types out. But it should be able to get the results into a table. I also don't know the dates as well.

USE [Dev]

--Run the Appraisal proc passing in a few required parameters 
IF OBJECT_ID('tempdb..#TempTbl') IS NOT NULL
BEGIN
    DROP TABLE #TempTbl
END
-- edit to declare the table's field types.
create table #TempTbl (	c.PortfolioBaseCode, c.ReportHeading1
					, a.SecTypeCode, b.Symbol, b.FullName
					, a.MarketValue, vs.IsUnsupervised,
						RptDate datetime ,
						RowID integer Identity(1,1))

Declare @ReportData varbinary(max)
Declare @RptDate as datetime

select @RptDate = '2014-02-28'
While @RptDate < '2014-03-31'
begin 

	Exec dbo.pAppraisal
	@ReportData = @ReportData out, 
	@Portfolios = '1234567', 
	@Date = @RptDate, 
	@IncludeUnsupervisedAssets = 1,
	@IncludeClosedPortfolios = 1

	insert into c.PortfolioBaseCode, c.ReportHeading1
				, a.SecTypeCode, b.Symbol, b.FullName
				, a.MarketValue, vs.IsUnsupervised, RptDate
	Select c.PortfolioBaseCode, c.ReportHeading1
	, a.SecTypeCode, b.Symbol, b.FullName
	, a.MarketValue, vs.IsUnsupervised, @RptDate
	From dbo.fAppraisal(@ReportData) a
	join dbo.vSec b on a.SecurityID = b.SecurityID
	join dbo.vSecVariant vs on vs.SecurityID = a.SecurityID AND vs.IsShort = a.IsShortPosition AND vs.SecTypeCode = a.SecTypeCode
	join dbo.vPortBase c on a.PortfolioBaseID = c.PortfolioBaseID
 
	select @RptDate = dateadd(day,1 , @RptDate)
end

select	* from #TempTbl

Open in new window

0
 

Author Closing Comment

by:KANEDA 0149
ID: 39973138
Thanks Jim, it worked perfectly when I defined my temp table.  Would I be able to contain all of this in another stored procedure?  Just wondering.
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 39973201
You should be able to wrap it into an SP:

CREATE PROCEDURE cp_Build_Appraisals @LastDate as datetime
-- I'm not sure how to get a table back from an SP off hand.
AS
BEGIN
...
-- I would change this chunk to the reporting periods you want
select @RptDate = dateadd(day,-30, @LastDate)
While @RptDate <= @LastDate
....
END

Open in new window


Something like this should work. You'll have to figure out how to return the resultant table. And I'm show how to do it on a thirty day from the input date. But you could also input a start and end date. Either way it should be relatively easy.
0
 

Author Comment

by:KANEDA 0149
ID: 39973479
Cool, thank you again Jim!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question