SQL 2005 Paging Using RowNumber()
Apr 22, 2008
I got problem with using custom paging in sql 2005
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[searchperson_view_general]
@Search nvarchar(2000)
,@OrderBy nvarchar (2000)
,@PageSize int
,@PageIndex int
AS
DECLARE @PageLowerBound int
DECLARE @PageUpperBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageSize - 1 + @PageLowerBound
--Default order by to first column
IF (@OrderBy is null or LEN(@OrderBy) < 1)
BEGIN
SET @OrderBy = 'p.[person_id]'
END
-- SQL Server 2005 Paging
declare @SQL as nvarchar(4000)
SET @SQL = 'WITH PageIndex AS ('
SET @SQL = @SQL + ' SELECT distinct'
IF @PageSize > 0
BEGIN
SET @SQL = @SQL + ' TOP ' + convert(nvarchar, @PageUpperBound)
END
SET @SQL = @SQL + ' ROW_NUMBER() OVER (ORDER BY ' + @OrderBy + ') as RowIndex '
SET @SQL = @SQL + ', p.[person_id]'
SET @SQL = @SQL + ', p.[userType_id]'
SET @SQL = @SQL + ', p.[fullName]'
SET @SQL = @SQL + ', p.[gender_nm]'
SET @SQL = @SQL + ', p.[dateOfBirth] '
SET @SQL = @SQL + ', p.[positionTitle]'
SET @SQL = @SQL + ' FROM dbo.[person_view] p '
IF LEN(@Search) > 0
BEGIN
SET @SQL = @SQL + @Search
END
SET @SQL = @SQL + ' ) SELECT distinct'
SET @SQL = @SQL + ' p.person_id'
SET @SQL = @SQL + ', p.userType_id'
SET @SQL = @SQL + ', p.fullName'
SET @SQL = @SQL + ', p.gender_nm'
SET @SQL = @SQL + ', (year(getdate()) - year(p.[dateOfBirth])) as [dateOfBirth] '
SET @SQL = @SQL + ', p.positionTitle'
SET @SQL = @SQL + ' FROM PageIndex p '
SET @SQL = @SQL + ' WHERE RowIndex > ' + convert(nvarchar, @PageLowerBound)
IF @PageSize > 0
BEGIN
SET @SQL = @SQL + ' AND RowIndex <= ' + convert(nvarchar, @PageUpperBound)
END
SET @SQL = @SQL + ' ORDER BY ' + @OrderBy
exec sp_executesql @SQL
I checked my store procedure with parameters
exec [hr2b_searchperson_view_general_load]
'LEFT OUTER JOIN qualification
ON p.person_id = qualification.person_id
WHERE qualification.institutionName like N''%ABC%'''
,' p.person_id asc ', 25 , 1
This is a actual query show :
WITH PageIndex AS
( SELECT distinct TOP 49 ROW_NUMBER() OVER
(ORDER BY p.person_id asc )
as RowIndex
, p.[person_id]
, p.[userType_id]
, p.[fullName]
, p.[gender_nm]
, p.[dateOfBirth]
, p.[positionTitle]
FROM person_view p
LEFT OUTER JOIN qualification
ON p.person_id = qualification.person_id
WHERE qualification.institutionName like N'%ABC%' )
SELECT distinct
p.person_id
, p.userType_id
, p.fullName
, p.gender_nm
, (year(getdate()) - year(p.[dateOfBirth])) as [dateOfBirth]
, p.positionTitle
FROM PageIndex p
WHERE RowIndex > 25 AND RowIndex <= 49 ORDER BY p.person_id asc
If I used this query without using DISTINCT it will return extactly number of records which I expected but It is duplicated.
Then I tried to use DISTINCT in this query number of records return is less than 25 records . Because it was duplicated records when I used LEFT OUTER JOIN.But my query will be able to use more LEFT OUTER JOIN than this query. Please help me get exactly 25 records?
This is my tables
person_view(person_id, fullname, userType_id, gender_nm, dateOfBirth, positionTitle)
Qualification(qualification_id, qualification_nm,institutionName, person_id)
Thanks in advance.
View 3 Replies
ADVERTISEMENT
Feb 13, 2008
Hi
Can any one tell me how to enable paging in ssrs 2005 reports, i am using "table" control for the report and when we print the report it gives every thing good, but when we desplay it the web it does not show page by page.
please help me...
thank you.............
View 5 Replies
View Related
May 29, 2006
lets say we have more than 100 000 rows in Table1, and we want to view each 10 rows alone.... and by pressing on a NEXT button we will see the other 10 pages....
there is 2 buttons : NEXT and PREVIOUS
so can anyone tell me how to do that in SQL 2005, and what is correctly called.
I have found a code that does use ROW_NUMBER in order to view results between 2 numbers,
example: rows between 10 and 50....
but It is not what I want, so please I need some help, thank you
By Uncle Sam
View 10 Replies
View Related
Apr 23, 2008
Hi,
I've got some procedure which pages select query, the example is below:
Code Snippet
CREATEEND PROC GetCustomersByPage
@PageSize int, @PageNumber int
AS
Declare @RowStart int
Declare @RowEnd int
if @PageNumber > 0
Begin
SET @PageNumber = @PageNumber -1
SET @RowStart = @PageSize * @PageNumber + 1;
SET @RowEnd = @RowStart + @PageSize - 1 ;
With Cust AS
( SELECT CustomerID, CompanyName, CompanyAddress,
ROW_NUMBER() OVER (order by CompanyName) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber >= @RowStart and RowNumber <= @RowEnd end
How can I change this procedure in order to page the query OVER the column set as an argument?
In other words I would like to execute proc like:
- exec GetCustomersByPage 10, 1, 'CompanyName' which pages by CompanyName(...OVER (order by CompanyName)...)
- exec GetCustomersByPage 10, 1, 'CompanyAddress' which pages by ComanyAddress
Is it possible?
View 8 Replies
View Related
May 29, 2006
lets say we have more than 100 000 rows in Table1, and we want to view each 10 rows alone.... and by pressing on a NEXT button we will see the other 10 pages....
there is 2 buttons : NEXT and PREVIOUS
so can anyone tell me how to do that in SQL 2005, and what is correctly called.
I have found a code that does use ROW_NUMBER in order to view results between 2 numbers,
example: rows between 10 and 50....
but It is not what I want, so please I need some help, thank you
By Uncle Sam
View 4 Replies
View Related
Jun 25, 2007
I have a webpage that displays 4000 or more records in a GridView control powered by a SqlDataSource. It's very slow. I'm reading the following article on custom paging: http://aspnet.4guysfromrolla.com/articles/031506-1.aspx. This article uses an ObjectDataSource, and some functionality new to Sql Server 2005 to implement custom paging.There is a stored procedure called GetEmployeesSubestByDepartmentIDSorted that looks like this:ALTER PROCEDURE dbo.GetEmployeesSubsetByDepartmentIDSorted( @DepartmentID int, @sortExpression nvarchar(50), @startRowIndex int, @maximumRows int)AS IF @DepartmentID IS NULL -- If @DepartmentID is null, then we want to get all employees EXEC dbo.GetEmployeesSubsetSorted @sortExpression, @startRowIndex, @maximumRows ELSE BEGIN -- Otherwise we want to get just those employees in the specified department IF LEN(@sortExpression) = 0 SET @sortExpression = 'EmployeeID' -- Since @startRowIndex is zero-based in the data Web control, but one-based w/ROW_NUMBER(), increment SET @startRowIndex = @startRowIndex + 1 -- Issue query DECLARE @sql nvarchar(4000) SET @sql = 'SELECT EmployeeID, LastName, FirstName, DepartmentID, Salary, HireDate, DepartmentName FROM (SELECT EmployeeID, LastName, FirstName, e.DepartmentID, Salary, HireDate, d.Name as DepartmentName, ROW_NUMBER() OVER(ORDER BY ' + @sortExpression + ') as RowNum FROM Employees e INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID WHERE e.DepartmentID = ' + CONVERT(nvarchar(10), @DepartmentID) + ' ) as EmpInfo WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @startRowIndex) + ' AND (' + CONVERT(nvarchar(10), @startRowIndex) + ' + ' + CONVERT(nvarchar(10), @maximumRows) + ') - 1' -- Execute the SQL query EXEC sp_executesql @sql ENDThe part that's bold is the part I don't understand. Can someone shed some light on this for me? What is this doing and why?Diane
View 4 Replies
View Related
Feb 7, 2008
Hi,
I'm using ComponentArt's Callback grids with Manual Paging.
The CA example grid uses Access:(http://www.componentart.com/webui/demos/demos_control-specific/grid/programming/manual_paging/WebForm1.aspx)
That SQL syntax produced is invalid in SQL Server 2005.
Example:
"SELECT TOP " & Grid1.PageSize & " * FROM (SELECT TOP " & ((Grid1.CurrentPageIndex + 1) * Grid1.PageSize) & " * FROM Posts ORDER BY " & sSortColumn & " " & sSortOrderRev & ", " & sKeyColumn & " " & sSortOrderRev & ") ORDER BY " & sSortColumn & " " & sSortOrder & ", " & sKeyColumn & " " & sSortOrder
So...This is what I have (simplified), and it appears return incorrect rows on the last few pages:
SELECT top 15 * FROM Posts where & sFilterString & " and Postid in (SELECT TOP " & ((Grid1.CurrentPageIndex + 1) * Grid1.PageSize) & " Postid FROM Posts where " & sFilterString & " ORDER BY " & sSortColumn & " " & sSortOrder & ") " & " ORDER BY " & sSortColumn & " " & sSortOrderRev
What other approaches has anyone used besides the "ID in (...)"?The examples I have included show the available variables: sort asc and desc, current page, number of rows on a page, etc.
View 2 Replies
View Related
Jul 24, 2006
heres my problem, since I migrated to SQL-Server 2005, I was able to use the Row_Number() Over Method to make my Custom Paging Stored Procedure better. But theres onte thing that is still bothering me, and its the fact the Im still using and old and classic Count instruction to find my total of Rows, which slow down a little my Stored Procedure. What I want to know is: Is there a way to use something more efficiant to count theBig Total of Rows without using the Count instruction??? heres my stored procedure:
SELECT RowNum, morerecords, Ad_Id FROM (Select ROW_NUMBER() OVER (ORDER BY Ad_Id) AS RowNum, morerecords = (Select Count(Ad_Id) From Ads) FROM Ads) as testWHERE RowNum Between 11 AND 20
The green part is the problem, the fields morerecords is the one Im using to count all my records, but its a waste of performance to use that in a custom paging method (since it will check every records, normally, theres a ton of condition with a lot of inner join, but I simplified things in my exemple)...I hope I was clear enough in my explication, and that someone will be able to help me. Thank for your time.
View 1 Replies
View Related
Aug 26, 2007
I am trying to write a stored procedure to be used for custompaging and I get error with the below SP. "Msg 207, Level 16, State 1, Procedure GetDealersPagedSP, Line 14 Invalid column name 'RowRank'."
What am I doing wrong?
CREATE PROCEDURE dbo.GetDealerSP
(@startRowIndex int,@maximumRows int)AsSELECT installersemaid,dealerid,[name],address1,address2,city,[state],zip,phone,faxFrom(SELECT installersemaid,dealerid,[name],address1,address2,city,[state],zip,phone,fax, ROW_NUMBER() OVER(ORDER BY [name] DESC)AS RowbankFROM dealerenrollment)as DealerWithRowNumbersWHERE Rowbank > @startRowIndex AND RowRank <= (@startRowIndex + @maximumRows)Go
View 2 Replies
View Related
Apr 26, 2007
I understand that if using this function with "nothing" between parentheses, then the running row total never resets.
However, I am hoping to have the row counter reset when a group value changes. I tried putting both the field and the defined group name in the parentheses both with and without quotes, but I get an error.
What is the correct syntax for accomplishing this?
View 1 Replies
View Related
May 14, 2008
Hi,
I have TWO columns i.e TIME and ACTION
i want to display RowNumber for that.
Like this...
TIME ACTION
12:40:01 BUY
12:40:31 BUY
12:40:51 BUY
12:41:01 SELL
12:41:21 SELL
12:41:31 SELL
12:41:41 SELL
12:41:51 BUY
12:42:01 BUY
12:42:29 SELL
12:42:31 SELL
12:42:41 SELL
12:42:51 BUY
It should display like this...
TIME ACTION RowNo
12:40:01 BUY 1
12:40:31 BUY 2
12:40:51 BUY 3
12:41:01 SELL 1
12:41:21 SELL 2
12:41:31 SELL 3
12:41:41 SELL 4
12:41:51 BUY 1
12:42:01 BUY 2
12:42:29 SELL 1
12:42:31 SELL 2
12:42:41 SELL 3
12:42:51 BUY 1
View 10 Replies
View Related
Apr 11, 2008
ID voucher amt
1 989 11.00
1 9876 234.00
2 677 678.99
3 789 3837
3 888 3733.77
3 890 66.43
I am trying to a rownumer using ROWNUMBER() OVER () function group by ID and order by voucher
The ourput should look like
ID voucher amt Linenum
1 989 11.00 00001
1 9876 234.00 00002
2 677 678.99 00001
3 789 3837 00001
3 888 3733.77 00002
3 890 66.43 00003
How can I do this? can you show me some example?
View 4 Replies
View Related
May 17, 2006
Hi All,
I have following table structure,
----------------------------------------------------------------------
ChallanID ProductID PublicationDate Description Qty Amt
----------------------------------------------------------------------
43 9 4/1/2006 ABC 1 880
43 10 5/1/2006 BCA 1 930
43 11 5/1/2006 CBA 1 230
I want a sql query which select all the record with a serial number eg:
---------------------------------------------------------------------
SN# ChallanID ProductID PublicationDate Description Qty Amt
----------------------------------------------------------------------
1 43 9 4/1/2006 ABC 1 880
2 43 10 5/1/2006 BCA 1 930
3 43 11 5/1/2006 CBA 1 230
View 5 Replies
View Related
Nov 29, 2007
Hi All
We have a requirement of creating a report which has one data set
And the same data set is being used accross different tables in the report. The tables differ by the type of grouping applied on them.
My situation is
1. The table has two groups applied
a) table level group is on DIVISION
b) detail grouping is done based on two columns:
SYSTEM and NAME
2. When i use the expression rownumber("DIVISION") or rownumber("SYSTEM_NAME"), it does not return a proper sequence
I need the row numbers to be generated based on the grouping on (SYSTEM and NAME) for each DIVISON
Can someone please explain how this can be done?
View 3 Replies
View Related
Nov 1, 2006
HI ...
I have a detailed report ..with summary lines and detailed lines (drill down).
I have a column with a function "RowNumber(Nothing)" which is supposed to just count the rows
when I put this in the summary row for the column I need in the Design Layout section and run the report, I get numbers on the summary lines which include the number of rows in the level below (detailed rows).
I just want to number the summary rows sequentially without taking into consideration, the number of detailed rows.
How do I modify RowNumber(Nothing) to exclude counting the detailed rows....???
Any help will be much appreciated...thanks
View 9 Replies
View Related
Apr 25, 2001
I want to show a row number for each row returned by a SELECT starting at 1 and incrementing by 1 for each row.
Anyone got any ideas?
View 2 Replies
View Related
Mar 27, 2008
Hi,
I create a matrix table with wizard. I want to write rownumber() as a new column near my column.
But when =Rownumber(nothing) it returns the value of the record in the dataset. If the data in the 56.row then
=Rownumber(nothing) =56
But in the preview of table it is in 1.row.
What can I do?
View 9 Replies
View Related
Apr 20, 2015
I have a requirement to find the rowmumbers based on a group. I know there is Rownumber function in sql. How can I implement the same in DAX. PFA screenshot. Rownumber is the calculated field i want based on the id and name column group.
View 8 Replies
View Related
Jul 5, 2000
Not nice but a work around: is there a way to get the amount of fields or rows from a table or query within MS SQL, to pick out the lastfield-4 for example ?
SELECT LASTFIELD-4
FROM CUSTOMERS
Thanks
View 1 Replies
View Related
Mar 17, 2014
I will try my best to explain this, We have a shopping cart on our website, the person that was developing this has now left the company and I've been given the job to finish it off.
When I load all the items that the user has entered in to his/her cart I return the Item ID and the RowNumber (ROW_NUMBER() OVER (Order by Id) AS RowNumber)
I'm trying to delete the item from the table using the following query
DELETE FROM [dbo].[Cart.Items] WHERE UniqueID = UniqueID and ItemID = @ItemID and @RowNumber IN (
SELECT ROW_NUMBER() OVER (Order by Id) AS RowNumber)
Now the reason we are using the RowNumber is because the user can add the same Item as many times as they like so for example you buy 3 different mobile phones, and you want three screen protectors, they will click screen protector 3 times which will add 3 records in to the db with the same id. so the row number is used to find the correct one.
But the above delete is not working.
View 1 Replies
View Related
Jul 6, 2015
I have the following table struction, lets call it table A.
bookidstartdate endate
2001 2000-01-01 2000-01-05
3001 2001-01-01 2001-01-02
4001 2002-01-01 2002-01-04
and i want the end result to be look like this in table B.
bookidstartdate endate bookidrowdate bookidlogseqrowsequence
2001 2000-01-012000-01-05 2000_01_01 2001_0 0
2001 2000-01-012000-01-05 2000_02_01 2001_1 1
2001 2000-01-012000-01-05 2000_03_01 2001_2 2
2001 2000-01-012000-01-05 2000_04_01 2001_3 4
3001 2002-02-01 2003-02-02 2000_01_01 3001_0 0
3001 2002-02-01 2003-02-02 2000_02_01 3001_1 1
4001 2002-01-01 2002-01-04 2002-01-01 4001_0 0
4001 2002-01-01 2002-01-04 2002-02-01 4001_1 1
4001 2002-01-01 2002-01-04 2002-02-01 4001_2 2
The script below works but i have a break when datediff (days,startdate, endate) reaches 0. For every bookidm i want to iterate till the datediff is zero then move on to next bookid and do the same thing.
declare @orders table
(
bookid int,
startdate date,
endate date,
rowsequence int
[Code] .....
View 9 Replies
View Related
Apr 26, 2007
I have a report with details grouping on table. What i need to do is put row number only on Parent row and skip the child row. When i use RowNumber("GroupName") of course it gives me a current RowNumber. Is there a way to count only parents?
View 3 Replies
View Related
Nov 8, 2006
Hi there
I have a sales report that is pulling up data and displaying the detailed lines and the aggregate/summary lines grouped by a single field.
THe report say has 10 summary/aggregate lines and each summary lines have a maybe 20 more lines to it.
I want to NUMBER the SUMMARY lines only. In a new column on the summary line cell, if I type, =RowNumber(Nothing), I get a count of all its sub-lines displayed. How can I limit the scope of numbering to just the summary lines and make sure it does not include the sub lines involved ?
View 7 Replies
View Related
Jun 5, 2007
hi all,
i am working on a small "Biztalk" engine, by creating dynamic ssis packages that change according to the client source file definition.
in order to create a row-number to each row in my input file, i am tryng to add the Konesans's Row Number component to the dynamic package by using SSIS API, but i get a lot of errors. the component is not created as a"rowNumber" component, but as a General Managed Component, though i use the ComponentClassID as the classID in the RowNumber component.
has anybody try to do this ?
is there any way to get the row number other then this way?
any ideas?
thank you!!! for all your help until now!!
View 1 Replies
View Related
Jun 8, 2015
I have got question because time is running but I still don't know how to do it.
So I have 1 group with 3 rows and I would like to put for each row not only group individual number like 1,2,3,4,5 ... etc until end of report. Generally using fuction RowNumber I got 1,2,3 and then 1,2,3 again.
View 9 Replies
View Related
Mar 14, 2008
I have to import data from a flatfile into our datawarehouse. The supplier of the flat file isn't able to give me a delta, he just gives me a flat files with all the changes on a certain table.
So for instance when a row in a table is updated 3 times, I get 3 rows in my flatfile with the 3 updates and I only need the last one.
The determine wich record is updated I need to combine 6 columns to be sure if I'm looking at the right row in the source database.
Is their a way to accomplish this?
I'm pretty new to those advanced SSIS things and I tried to look for a way to do it with conditional splits etc...
But honestly, I don't have a clue where to start.
Any help is appreciated.
Regards
Zekske
View 1 Replies
View Related
Oct 12, 2004
Is there any way to implement a paging scheme such that only the required records are transferred from SQL Server to the asp.net app?
The only support I can find such as the DataAdaptor.Fill will bring all the records back from SQL Server and then create the page...
This obviously still takes time and memory based on the entire query, not the page size.
Any ideas?
View 2 Replies
View Related
Jul 26, 2005
I have a table with a lot of records. I want to make paging without passing all the data back to a dataset. I know how to select the top n rows but how do I select 10-20 for example.
View 3 Replies
View Related
Dec 9, 2005
Hello,
How can I do paging in my SQL Server stored procedure.
I have thought of a way, but I need to :
"SELECT TOP @Count..."
which is not allowed :S
What can I do to get around this?
View 1 Replies
View Related
Dec 11, 2003
I have noticed that the server i'm running SQL2k on is starting to page out of the norm. I can see that the regsvc and sqlservr svc are showing high page faults/sec. I have 3 gigs of ram and set the max that sql can use to 2 gigs. It is currently using only 168 MB and still will show high paging at random times. I know I can add more ram but that doesn't seem to be the problem. I have also stopped unnecessary services at the os level.
Any other suggestions to fix this?
Thanks in advance.
View 6 Replies
View Related
May 9, 2008
Hi there
I've managed to make it query that return a dataset that have 2 views utilising the "Filter" in RS. I treat this as a single record with multiple views.
Now let say if I have a stored precedure that pass 2 parameters one is called state and year and accepting 'ALL' for every possibility of State and Year and construct that into single dataset with 2 views similar like above.
How do I breakdown this in the reporting services so it will have paging?
This is a simple dataset:
RECORDID, ReportViewType, State, Year, VALUE
1, "VIEW1", "NSW", 1, null
1, "VIEW2", null, null, 10000
2, "VIEW1", "NSW", 2, null
2, "VIEW2", null, null, 11000
3, "VIEW1", "VIC", 1, null
3, "VIEW2", null, null, 11003
4, "VIEW1", "VIC", 2, null
4, "VIEW2", null, null, 11001
I would like to break down (paging) this per recordid. Each page obviosuly has 2 views using the same data set with different FILTER.
Do I need to put into a LIST then inside that list put 2 TABLES? Is this possible?!?!
Thanks
View 5 Replies
View Related
Aug 2, 2006
I have created a stored proc for paging on a datalist which uses a objectDataSource.
I have a output param itemCount which should return the total rows. Them I am creating a temp table to fetch the records for each page request. My output param works fine if I comment out all the other select statements. But returns null with them. Any help would be appreciated.
CREATE PROCEDURE [dbo].[CMRC_PRODUCTS_GetListByCategory]( @categoryID int, @pageIndex INT, @numRows INT, @itemCount INT OUTPUT )AS
SELECT @itemCount= COUNT(*) FROM CMRC_Products where CMRC_Products.CategoryID=@categoryID Declare @startRowIndex INT; Declare @finishRowIndex INT; set @startRowIndex = ((@pageIndex -1) * @numRows) + 1; set @finishRowIndex = @pageIndex * @numRows
DECLARE @tCat TABLE (TID int identity(1,1),ProductID int, CategoryID int, SellerUserName varchar(100), ModelName varchar(100), Medium varchar(50),ProductImage varchar(100),UnitCost money,Description varchar(1500), CategoryName varchar(100), isActive bit,weight money)
INSERT INTO @tCat(ProductID, CategoryID,SellerUserName,ModelName,Medium,ProductImage,UnitCost,Description,CategoryName, isActive,weight)SELECT CMRC_Products.ProductID, CMRC_Products.CategoryID, CMRC_Products.SellerUserName, CMRC_Products.ModelName, CMRC_Products.Medium,CMRC_Products.ProductImage, CMRC_Products.UnitCost, CMRC_Products.Description, CMRC_Categories.CategoryName, CMRC_Products.isActive,CMRC_Products.weightFROM CMRC_Products INNER JOIN CMRC_Categories ON CMRC_Products.CategoryID = CMRC_Categories.CategoryIDWHERE (CMRC_Products.CategoryID = @categoryID) AND (CMRC_Products.isActive = 1)
SELECT ProductID, CategoryID,SellerUserName,ModelName,Medium,ProductImage,UnitCost,Description,CategoryName, isActive,weightFROM @tCat WHERE TID >= @startRowIndex AND TID <= @finishRowIndexGO
View 12 Replies
View Related
Dec 13, 2006
I am using a stored procedure to page some objectsThe procedure looks like this:
CREATE PROCEDURE sw20aut.sw20_Kon( @sid_nr INT, @sid_stl INT = 35, @kid int )AS BEGIN SET NOCOUNT ON DECLARE @rader INT, @sid_antal INT, @ubound int, @lbound int SELECT @rader = COUNT(*), @sid_antal = COUNT(*) / @sid_stl FROM sw20aut.sw_kontakter WITH (NOLOCK) WHERE kund_id = @kid AND del = '0'
IF @rader % @sid_stl != 0 SET @sid_antal = @sid_antal + 1 IF @sid_nr < 1 SET @sid_nr = 1 IF @sid_nr > @sid_antal SET @sid_nr = @sid_antal SET @ubound = @sid_stl * @sid_nr IF(@sid_antal > 0) SET @lbound = @ubound - (@sid_stl - 1) ELSE SET @lbound = 0 SELECT CurrentPage = @sid_nr, TotalPages = @sid_antal, TotalRows = @rader
DECLARE @ename VARCHAR(64), @fname VARCHAR(64), @konid VARCHAR(64) SET ROWCOUNT @lbound SELECT @ename = enamn, @fname = fnamn, @konid = kon_id FROM sw20aut.sw_kontakter WITH (NOLOCK) WHERE kund_id = @kid AND del = '0' ORDER BY enamn, fnamn, kon_id SET ROWCOUNT @sid_stl SELECT kon_id, enamn, fnamn FROM sw20aut.sw_kontakter WITH (NOLOCK) WHERE enamn + fnamn + '~' + CAST(kon_id as VARCHAR(64)) >= @ename + @fname + '~' + @konid AND (kund_id = @kid AND del = '0') ORDER BY enamn, fnamn, kon_id SELECT startid = @konid SET ROWCOUNT 0END
The big problem is that i need to display objet with the same name. In my book the best identifier is the PK and therefor i have sorted as above by ordering after LastName, FirstName, ContactId
After som thinking ive reached the conclusion that this dont work if the idnumbers isnt of the same length. as long as they are(for example two people named John Smith, one with id = '23' and one with id = '87' it works. If there ids would have been '23' and '1203' it will not work correctly) of the same length it works fine.
What im wondering is if anyone have a good solution to this? Only thing i can think of is filling all idnumbers with zeros to equal length. Dont know how and if this will affect performance though. Anyone has a practical solution to this?
View 1 Replies
View Related