December 15, 2014 at 3:51 pm
I have been searching and I can't quite figure this out. I am very new to this...
I am trying to create a query that fills a report. This report needs to show 15 rows per page, making it wrap after 15 rows is easy. If the query yields 5 results then I need 10 blank rows joined at the end or if it goes over 15 then the final row count must always be divisible by 15.
This is because I am replacing a hand written report that must look exactly like the original, plus the blank rows will be available for writing in new information until the data can be entered.
I have no idea how to do this. I've tried a bunch of things but can't get to the end. What I am trying to do right now is get my query and a count then use a case statement to union the desired amount of additional rows.
SELECT
dbo.STOCK.BARCODEID,
dbo.STOCK.PARENT_BARCODEID,
dbo.STOCK.NAME,
dbo.STOCK.STORAGE,
dbo.STOCK.NOTES_
FROM
dbo.STOCK
WHERE
dbo.STOCK.BARCODEID = '<%@EID|||S|True%>' AND NOTES_ <> ''
UNION ALL
SELECT NULL, NULL, NULL, NULL, '~~~', NULL
ORDER BY
dbo.STOCK.STORAGE
Yes, this is not close to what I'm asking for but I backed it up so that it still works.
December 15, 2014 at 4:12 pm
This is an idea on how to do it. It's not perfect as the NULLS will be sorted first. I'm sure that you'll be able to handle that.
DECLARE @RowCount int;
SELECT TABLE_CATALOG
,TABLE_SCHEMA
,TABLE_NAME
,COLUMN_NAME
,ORDINAL_POSITION
INTO #Columns
FROM INFORMATION_SCHEMA.columns;
SET @RowCount = @@ROWCOUNT;
WITH cteShort(N) AS(
SELECT TOP(15 - (@RowCount % 15)) N
FROM(VALUES(NULL),(NULL),(NULL),(NULL),(NULL), --5
(NULL),(NULL),(NULL),(NULL),(NULL), --10
(NULL),(NULL),(NULL),(NULL),(NULL))e(N) --15
)
SELECT *
FROM #Columns
UNION ALL
SELECT NULL
,NULL
,NULL
,'~~~'
,NULL
FROM cteShort
ORDER BY TABLE_CATALOG DESC;
DROP TABLE #Columns;
February 8, 2015 at 6:18 pm
Luis Cazares (12/15/2014)
This is an idea on how to do it. It's not perfect as the NULLS will be sorted first. I'm sure that you'll be able to handle that.
DECLARE @RowCount int;
SELECT TABLE_CATALOG
,TABLE_SCHEMA
,TABLE_NAME
,COLUMN_NAME
,ORDINAL_POSITION
INTO #Columns
FROM INFORMATION_SCHEMA.columns;
SET @RowCount = @@ROWCOUNT;
WITH cteShort(N) AS(
SELECT TOP(15 - (@RowCount % 15)) N
FROM(VALUES(NULL),(NULL),(NULL),(NULL),(NULL), --5
(NULL),(NULL),(NULL),(NULL),(NULL), --10
(NULL),(NULL),(NULL),(NULL),(NULL))e(N) --15
)
SELECT *
FROM #Columns
UNION ALL
SELECT NULL
,NULL
,NULL
,'~~~'
,NULL
FROM cteShort
ORDER BY TABLE_CATALOG DESC;
DROP TABLE #Columns;
Except that I believe the Table Row Constructor (VALUES) was introduced in SQL 2008.
Instead you can use something like this:
SELECT NULL UNION ALL SELECT NULL
... -- 11 more
UNION ALL SELECT NULL UNION ALL SELECT NULL
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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply