Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get The Rows Count for 'With' table before using it ?? Expand / Collapse
Author
Message
Posted Monday, November 25, 2013 10:26 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 7:16 AM
Points: 15, Visits: 56
I want to get the total number of records for with Table before using it in a variable .. So any Ideas for how can I handle that
DECLARE @totalCount int
;WITH tmpTable AS (SELECT DISTINCT * ,ROW_NUMBER() OVER ( ORDER BY InventorySys ) AS RowNumber FROM dbo.Inventory)

SELECT @totalCount = Count(*) FROM tmpTable

SELECT * , @totalCount as RecordsCount
FROM tmpTable
WHERE (RowNumber BETWEEN @StartIndex AND @EndIndex) OR @StartIndex = 0 OR @EndIndex = 0
ORDER BY RowNumber

but I get this error Invalid object name 'tmpTable'.

I can handle it using SubQuery but i don't like that way a lot "SubQuery"

 ;WITH tmpTable AS (SELECT DISTINCT *  ,ROW_NUMBER() OVER (  ORDER BY  InventorySys  ) AS RowNumber  FROM dbo.Inventory) 

SELECT * , (SELECT Count(*) FROM tmpTable) as RecordsCount
FROM tmpTable
WHERE (RowNumber BETWEEN @StartIndex AND @EndIndex) OR @StartIndex = 0 OR @EndIndex = 0
ORDER BY RowNumber

So any Ideas for how can I handle that without using subquery Solution ???
Post #1517361
Posted Monday, November 25, 2013 10:31 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:54 PM
Points: 3,637, Visits: 7,932
It's basically the same as using the subquery but using another CTE.

WITH tmpTable AS (
SELECT DISTINCT *,
ROW_NUMBER() OVER ( ORDER BY InventorySys ) AS RowNumber
FROM dbo.Inventory
),
Total AS(
SELECT totalCount = Count(*)
FROM tmpTable
)
SELECT tmp.* , tot.totalCount as RecordsCount
FROM tmpTable tmp
CROSS JOIN Total tot
WHERE (tmp.RowNumber BETWEEN 1 AND 6) OR 1 = 0 OR 6 = 0
ORDER BY tmp.RowNumber




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1517364
Posted Monday, November 25, 2013 10:33 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:54 PM
Points: 3,637, Visits: 7,932
I just noticed the additional conditions on your WHERE clause that will always evaluate to false
 OR 1 = 0 OR 6 = 0 

Why do you even have them? What were you trying to accomplish?



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1517367
Posted Monday, November 25, 2013 5:37 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:55 PM
Points: 3,648, Visits: 5,321
And then there's this:

SELECT DISTINCT *  ,ROW_NUMBER() OVER (  ORDER BY  InventorySys  )


The DISTINCT has no meaning here (just drags down the performance) because the ROW_NUMBER() will ensure that each record is distinct.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1517502
Posted Tuesday, November 26, 2013 3:22 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 7:16 AM
Points: 15, Visits: 56
Yes ..sorry for that ..It shall be startIndex and EndIndex Paramaters
Post #1517586
Posted Tuesday, November 26, 2013 3:27 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 28, 2014 7:16 AM
Points: 15, Visits: 56
OK ..Thanks a lot "dwain.c" .. Yes your are right ..but that was just try to simplify my real script .. The Real Script has another columns order by ..But thanks a lot for your note :)
Post #1517589
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse