|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 8:50 AM
Points: 74,
Visits: 438
|
|
Hugo Kornelis (5/7/2008)
Shamshad Ali (5/7/2008) Thank you all for your valuable replies. I would like to appreciate if some one make a scenario (simplest one mapped to my problem) and provide me solution/steps to be developed one by one. I need some sort of code + hints that may improve performance by removing Cursor, if possible.I would like to appreciate help from Jeff and Christopher - Plz. Not from me? 
haha, well...you do kind of sound crazy you know...have you read your blog lately? Downed one too many energy drinks I think. =)
EDIT:
(P.S. But it's all extremely great info Hugo...didn't want you to think I was insulting you)
Maybe I should put down the energy drinks....
Christopher Ford
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 8:50 AM
Points: 74,
Visits: 438
|
|
Shamshad,
Unfortunately, your question and explanation are still too broad to provide code or examples.
You mentioned you're calling a C# (ver. 3.5) DLL??
If you've started down that path, you might as well start using SQL Server Integration Services (SSIS) to do this nightly job that you have. This would be a much better solution than using a Tally table.
You can use SSIS to remove your cursor logic essentially doing everything in one pass, plus it will be in managed code.
You wouldn't need to migrate any current databases, just need to install Integration Services and the Business Intelligence Studio that comes with SQL 2005 to author your package. You could then invoke the custom package in .Net without running SQL 2005.
That's if you're bent on sticking with SQL 2000.
But, in short, my vote is no...I have no idea how to resolve your problem without upgrading you to a better Database System. :)
Christopher Ford
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 5:02 AM
Points: 2,365,
Visits: 1,825
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Thursday, January 12, 2012 10:21 PM
Points: 526,
Visits: 588
|
|
Well, very first i did thank to all, and ur included in that. But i saw some one is providing me a hint(looks like dreams comes true), so i put those two names coz they understand exactly wat my problem was and if someone know the problem then he post replies. So I would like if some spend time providing me that hint in practically proving the solution.
Thanks to every one once again.
I really apprecaite if some one take action and help me solve my problem (obviously first knowing the scenaio and making its solution)
Shamshad Ali.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 2:56 AM
Points: 2,008,
Visits: 2,469
|
|
Jeff,
Excellent Article!
It seems that you have written for me at the right time.:) Really, It is very useful for me.
Because so far i have replaced more than 5 cursors and used Tally table in that places.
Thank You Sql teacher.:)
karthik
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, April 06, 2013 6:59 AM
Points: 1,
Visits: 27
|
|
How about using recursion to replace loops? (One of the great new functions in SQL2005)
No need for a tally or temporary tables.
Heres a params example:
DECLARE @Params varchar(8000) SET @Params = '1,2,3,4,5,6,7,8,9,10';
WITH Params AS ( -- start with last item
SELECT REVERSE(SUBSTRING(REVERSE(@Params), 0, charindex(',', REVERSE(@Params)))) as [value], len(@Params) as start
UNION ALL
-- base case SELECT SUBSTRING(@Params, 0, charindex(',', @Params)) as [value], charindex(',', @Params) + 1 as start
UNION ALL
-- recursive case SELECT SUBSTRING(@Params, start, charindex(',', @Params, start) - start) as [value], charindex(',', @Params, start) + 1 as start FROM Params WHERE charindex(',', @Params, start) > 0
) SELECT * FROM Params OPTION (MAXRECURSION 999); -- must be greater than max number of items
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 2,224,
Visits: 4,083
|
|
Another great article ! Thanks
The first reference for a tally table that I can recall is in "Guide to Sybase and SQL Server" by C. J. Date and D.McGoveran published in June of 1992.
Does anyone know of an earlier reference ?
SQL = Scarcely Qualifies as a Language
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, April 24, 2013 12:11 PM
Points: 143,
Visits: 183
|
|
This is a super cool article. Let me repeat, this is a super cool article. What a refreshing read!
I look back on all the TSQL I have written using loops or cursors - arrrgh! Oh, and all the arguments/discussions on cursors, loops, variables of type table....
I am hooked on Tally tables. It is an extremely obvious in-your-face concept and I can't imagine why I haven't used this technique before.
Thanks again.
-Mike
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, October 31, 2012 3:59 AM
Points: 215,
Visits: 166
|
|
This is slick...... Great work Jeff!
This is a great example of implementing set-based programming, three thumbs up from me. Now to apply this to problems going forward!
Thanks for the excellent article and insight into this subject.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 2,224,
Visits: 4,083
|
|
From the article, you wrote " Tally table .. starting at 0 or 1 (mine start at 1)" There are advantages for having the Tally table include 0 and the "Freight by Date" problem becomes easier if 0 is included.
The original SQL solution contains:
SELECT t.N-1+@DateStart AS ShippedDate FROM dbo.Tally t WHERE t.N-1+@DateStart <= @DateEnd
This SQL snippet has "N - 1", which derives the tally to start with 0. Wouldl it not be simplier to have the tally table already contain 0?
The original SQL solution has a problem that if the tally table does have a row for zero, the results are different (there is an extra day before the oldest shipment).
The original SQL solution has been modified to eliminate all variables so that the solution can be used in a view, does not assume that the tally table has 0 but allows the tally table to contain 0:
SELECT dates.ShippedDate , COALESCE( SUM(o.Freight) , 0) AS TotalFreight FROM dbo.Orders o RIGHT OUTER JOIN (SELECT t.N - 1 + Shipments.DateStart AS ShippedDate FROM dbo.Tally t JOIN (SELECT MIN(ShippedDate) AS DateStart , MAX(ShippedDate) AS DateEnd FROM dbo.Orders ) AS Shipments ON t.N BETWEEN 1 AND ( Shipments.DateEnd - Shipments.DateStart + 1 ) ) AS dates ON o.ShippedDate = dates.ShippedDate GROUP BY dates.ShippedDate ORDER BY dates.ShippedDate
Here is revised SQL that assumes that the tally table has 0. The solution is slight simplier with the original SQL as comments and bold.
SELECT dates.ShippedDate , COALESCE( SUM(o.Freight) , 0) AS TotalFreight FROM dbo.Orders o RIGHT OUTER JOIN -- (SELECT t.N - 1 + Shipments.DateStart AS ShippedDate (SELECT t.N + Shipments.DateStart AS ShippedDate FROM dbo.Tally t JOIN (SELECT MIN(ShippedDate) AS DateStart , MAX(ShippedDate) AS DateEnd FROM dbo.Orders ) AS Shipments -- ON t.N BETWEEN 1 AND ( Shipments.DateEnd - Shipments.DateStart + 1 ) ON t.N BETWEEN 0 AND (Shipments.DateEnd - Shipments.DateStart ) ) AS dates ON o.ShippedDate = dates.ShippedDate GROUP BY dates.ShippedDate ORDER BY dates.ShippedDate
SQL = Scarcely Qualifies as a Language
|
|
|
|