Take SQL Cursor to Set Base

I have a cursor that goes through a table finds any date with null data, and populates that date with yesterday's values.  The date with null data will only have 1 row, but the previous day may have up to  20 rows. (The date field has a many to many relation).    There may be more than one day in a row  (sequence not table row) that is null (weekends and holidays).  In this case we need to use the last available day and populate the null dates with this data until the next day with data.

Below is how I'm doing it in the cursor.

 
Declare @Table varchar(20) = 'HoldingTable' --This line isn't there normally, I actually
--pass the table name in dynamically; however, for clarity sake, I added this here.

Set @strSQL = 'Select Cast(DayYYYYMMDD as Date) as DayYYYYMMDD
		Into Staging_Cursor
		From ' + @Table +
		' Where FullSymbol is null
		Order by DayYYYYMMDD'
	Exec (@strSQL)


DECLARE CursorDB  Cursor  For
		Select DayYYYYMMDD
		From Staging_Cursor		
		Order by DayYYYYMMDD


	--*****************************************************************
	--Assigns missing days to variables
	Open CursorDB
	Fetch Next From  CursorDB into  @MissingDay
								

	While @@FETCH_STATUS=0
	Begin

		--
		--Get The correct values to insert into missing day
		Set @strSQL_Insert = 
			'Insert Into ' + @Table +
		   ' Select A.DayYYYYMMDD,
			B.FullSymbol 
			,B.TickerSymbol 
			,B.LastPrice
			,B.LastTrade
			,B.CurrentContract
			From ' + @Table + ' A
			Join ' + @Table + ' B ON
				DateAdd(day,-1,''' + @MissingDay + ''') = Cast(B.DayYYYYMMDD as Date)  AND
				''' + @MissingDay + ''' = Cast(A.DayYYYYMMDD as Date)'

			
			Exec sp_executesql @strSQL_Insert

	Fetch Next From  CursorDB into  @MissingDay
	End

	Close CursorDB
	Deallocate CursorDB

Open in new window

LVL 1
badrhinoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
badrhinoConnect With a Mentor Author Commented:
Guys,

I ended up combining the two.  Essentially I put the join like Scott suggested in a while loop (Using a while loop) and Insert each set of missing days (e.g. all Saturdays, then on the next iteration all Sundays, etc.).  While still a loop, it is only going to loop at most 4-5 times.  

Thanks for the help!
0
 
LowfatspreadCommented:
ok so what is your problem?

ps.  why are you
1) using a cursor for this (v slow in t-sql)
2) using dynamic sql (insecure)


... background info would help....
what does the system represent?
some example initial data, and the corrected data you would expect...
is this for production use or a one-off...
how much data do you expect to be in the tables...
what is the frequency you expect to run this on...
confirm which version/edition of sql server this is to processed on.
0
 
Scott PletcherConnect With a Mentor Senior DBACommented:
Forget the cursor: unlike for Oracle, cursors are performance destroyers in SQL Server.  Just do something like this instead:


Declare @Table varchar(20) = 'HoldingTable' --This line isn't there normally, I actually
Declare @strSQL_Insert varchar(8000)

Set @strSQL_Insert = REPLACE('
Insert Into $table$
Select Cast(A.DayYYYYMMDD As date) As DayYYYYMMDD
                  ,B.FullSymbol
                  ,B.TickerSymbol
                  ,B.LastPrice
                  ,B.LastTrade
                  ,B.CurrentContract
From $table$ A
Inner Join $table$ B On
     Cast(B.DayYYYYMMDD as Date) = DateAdd(Day, -1, Cast(A.DayYYYYMMDD As date))
Where A.FullSymbol Is Null
', '$table$', ISNULL('[' + PARSENAME(@Table, 2) + '].', 'dbo.') + '[' + PARSENAME(@Table, 1) + ']')

PRINT @strSQL_Insert
--EXEC (@strSQL_Insert)
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
badrhinoAuthor Commented:
LowFat,

My problem is I want to replace the cursor with a set base t-sql.  I'm using dynamic sql because I have to use this multiple times for multiple commodities.  So if it isn't dynamic I have to have at a minimum 3 cursors with the exact same code except a different table name injection isn't really an issue because I'm the only one that will see this.

I'm running sql server 2012, the number of times this will be ran is unknown at this time (at least once), but because it may be ran once a week or more (and for the plain fact I want to try and not use a cursor) I would like to rewrite this.  This is for a production server and the number of rows will grow but as of right now we are looking at only 200,000 observations.

The data is actually cme futures data with each day have x number of contracts for each commodity.  

Data before cursor:

DayYYYYMMDD	FullSymbol	LastPrice	LastTrade	CurrentContract
19960102	FC1996F	60.35	1/2/1996	1
19960102	FC1996H	58.08	1/2/1996	2
19960102	FC1996J	58.13	1/2/1996	3
19960102	FC1996K	58.33	1/2/1996	4
19960102	FC1996Q	60.05	1/2/1996	5
19960102	FC1996U	59.9	1/2/1996	6
19960102	FC1996V	59.75	1/2/1996	7
19960102	FC1996X	60.75	1/2/1996	8
19960103	null	null 	null	null
19960104	FC1996F	59.98	1/4/1996	1
19960104	FC1996H	58.08	1/4/1996	2
19960104	FC1996J	58.05	1/4/1996	3
19960104	FC1996K	58.25	1/4/1996	4
19960104	FC1996Q	59.93	1/4/1996	5
19960104	FC1996U	59.98	1/4/1996	6
19960104	FC1996V	60	1/4/1996	7
19960104	FC1996X	61.05	1/4/1996	8

Open in new window


Data after Cursor:
DayYYYYMMDD	FullSymbol	LastPrice	LastTrade	CurrentContract
19960102	FC1996F	60.35	1/2/1996	1
19960102	FC1996H	58.08	1/2/1996	2
19960102	FC1996J	58.13	1/2/1996	3
19960102	FC1996K	58.33	1/2/1996	4
19960102	FC1996Q	60.05	1/2/1996	5
19960102	FC1996U	59.9	1/2/1996	6
19960102	FC1996V	59.75	1/2/1996	7
19960102	FC1996X	60.75	1/2/1996	8
19960103	FC1996F	60.55	1/3/1996	1
19960103	FC1996H	58.33	1/3/1996	2
19960103	FC1996J	58.35	1/3/1996	3
19960103	FC1996K	58.55	1/3/1996	4
19960103	FC1996Q	60.35	1/3/1996	5
19960103	FC1996U	60.15	1/3/1996	6
19960103	FC1996V	60.18	1/3/1996	7
19960103	FC1996X	61.18	1/3/1996	8
19960104	FC1996F	59.98	1/4/1996	1
19960104	FC1996H	58.08	1/4/1996	2
19960104	FC1996J	58.05	1/4/1996	3
19960104	FC1996K	58.25	1/4/1996	4
19960104	FC1996Q	59.93	1/4/1996	5
19960104	FC1996U	59.98	1/4/1996	6
19960104	FC1996V	60	1/4/1996	7
19960104	FC1996X	61.05	1/4/1996	8

Open in new window

0
 
LowfatspreadCommented:
what updates fullsymbol... to stop rows always being added for the null condition?
0
 
badrhinoAuthor Commented:
Guys,

My data after cursor should is a little off.  For all  rows of '19960103' the LastTrade should be '1/2/1996' not '1/3/1996'  Sorry about that.
0
 
Scott PletcherSenior DBACommented:
Did you try my non-cursor code?
0
 
badrhinoAuthor Commented:
Fullsymbol and the other fields are coming from a CME feed.  To get the null rows I'm joiningg the CME table to a numbers table:
	;With Staging1 as (
		Select * 
		  From Contract_Reference_Stage1

		)

		Select *
		From Days A   --Numbers table
		Left Join Staging1 B ON
			Cast(A.DayYYYYMMDD as Date) = B.LastTrade 

Open in new window


I've stripped all the conditions out so we can focus on the issue.  There is a valid reason why I'm using a CTE, but didn't want to clutter the issue.
0
 
badrhinoAuthor Commented:
Scott,

Not yet.  I had to put a fire out.  I'm trying it right now.
0
 
badrhinoAuthor Commented:
Scott,

Looking at your code, it appears that we're replacing the table name.  If this is what is happening, I don't think I was clear on what the cursor was being used for (My fault, not very good at explaining things).   The dynamic portion is because I'm running it multiple times for different commodities (thus different tables).  The cursor portion is actually going through each row, finding the days that have null values and then inserting the previous days values for that day.
0
 
Scott PletcherSenior DBACommented:
I believe my code does that but in a set-based fashion.  You can do all missing dates in one statement, you don't have to go day by day.
0
 
badrhinoAuthor Commented:
Great.  Lets dig into then.  I'm having a hard time following it--Why do we use the replace function?
0
 
badrhinoAuthor Commented:
I think I'm following it now.  Let me run it and see what happens!
0
 
badrhinoAuthor Commented:
Scott,

Your code semi works.  For instance if there are two day's in a row that have null data it inserts the first day because the previous day had data (Friday).  However, because Sunday's previous day (Saturday) doesn't have any data, it is null still.  This is the problem I was trying to overcome before I went to a cursor.  Any ideas?
0
 
badrhinoAuthor Commented:
Just a note, we also can't be in the mindset that it will always be two day islands.  For instance, if there is a Holiday it could 3-4 days.
0
 
badrhinoAuthor Commented:
I had done Scotts solution before I came to expers-exchange; however, I wouldn't have come to my solution without Scott's input.  That's why I accepted this as the solution, and Scott got the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.